Skip to content

Commit

Permalink
refactor(overlays): wrap JetBrains IDEs in derivation to avoid unnece…
Browse files Browse the repository at this point in the history
…ssary re-builds
  • Loading branch information
zakuciael committed Dec 8, 2024
1 parent b5a1ddb commit d59fde4
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 49 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
"usbhid",
"vesktop",
"vfat",
"vmoptions",
"vmopts",
"waybar",
"webstorm",
Expand Down
6 changes: 3 additions & 3 deletions modules/dev/ide.nix
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

availableIdes = builtins.listToAttrs (
builtins.map (value: {
name = value.pname;
name = value.baseName or value.pname;
inherit value;
})
(with pkgs.jetbrains; [
Expand Down Expand Up @@ -99,13 +99,13 @@
(mkIDE webstorm {})
])
);
installedIDEs = builtins.map (name: availableIdes.${name} or availableIdes."${name}-with-plugins") cfg;
installedIDEs = builtins.map (name: availableIdes.${name}) cfg;
in {
options.modules.dev.ides = mkOption {
description = "A list of JetBrains IDEs names to install";
example = ["rust-rover" "webstorm"];
default = [];
type = with types; listOf (enum (builtins.map (name: builtins.replaceStrings ["-with-plugins"] [""] name) (builtins.attrNames availableIdes)));
type = with types; listOf (enum (builtins.attrNames availableIdes));
};

config = mkIf (cfg != []) {
Expand Down
112 changes: 69 additions & 43 deletions overlays/jetbrains/ides/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,19 @@
{lib, ...}: let
supportedIdes = [
"clion"
"datagrip"
"dataspell"
"gateway"
"goland"
"idea-ultimate"
"mps"
"phpstorm"
"pycharm-professional"
"rider"
"ruby-mine"
"rust-rover"
"webstorm"
];
in [
(final: prev: let
inherit (lib) optionalString;
Expand All @@ -20,8 +35,9 @@ in [
};
})
(final: prev: let
inherit (lib) optionalString optionalAttrs;
inherit (lib) optionalAttrs attrValues toLower;
inherit (lib.my.mapper) toJavaProperties;
inherit (final) stdenv writeText;

mkPatchedJetbrainsProductDerivation = name: {
vmopts ? null,
Expand All @@ -30,63 +46,73 @@ in [
plugins_path ? null,
logs_path ? null,
extraProperties ? null,
}: let
pkg =
} @ opts: let
shouldOverride = builtins.any (opt: opt != null) (attrValues opts);
ide =
if !prev.jetbrains ? "${name}"
then throw "JetBrains IDE with name ${name} is not in nixpkgs"
else prev.jetbrains."${name}";
in
(pkg.override {
inherit vmopts;
})
.overrideAttrs (prevAttrs: rec {
ideaPropertiesIDE = pkg.vmoptsIDE;
ideaPropertiesFile =
optionalString
(config_path != null || caches_path != null || plugins_path != null || logs_path != null || extraProperties != null)
(
toJavaProperties "idea.properties" ({}
if shouldOverride
then
stdenv.mkDerivation rec {
inherit (ide) meta version buildNumber;
pname = meta.mainProgram + "-with-opts";
baseName = meta.mainProgram;
src = ide;
dontInstall = true;
dontFixup = true;
nativeBuildInputs = ide.nativeBuildInputs or [];
buildInputs = ide.buildInputs or [];

buildPhase = let
rootDir =
if stdenv.hostPlatform.isDarwin
then "Applications/${ide.product}.app/Contents"
else meta.mainProgram;
ideaPropertiesFile = toJavaProperties "${meta.mainProgram}" ({}
// optionalAttrs (config_path != null) {"idea.config.path" = config_path;}
// optionalAttrs (caches_path != null) {"idea.system.path" = caches_path;}
// optionalAttrs (plugins_path != null) {"idea.plugins.path" = plugins_path;}
// optionalAttrs (logs_path != null) {"idea.log.path" = logs_path;}
// optionalAttrs (extraProperties != null) extraProperties)
);
// optionalAttrs (extraProperties != null) extraProperties);

hiName =
if ide.vmoptsIDE == "WEBIDE"
then "WEBSTORM"
else ide.vmoptsIDE;
loName = toLower hiName;
vmoptsName =
loName
+ lib.optionalString stdenv.hostPlatform.is64bit "64"
+ ".vmoptions";
vmoptsFile = lib.optionalString (vmopts != null) (writeText vmoptsName vmopts);
in ''
cp -r ${ide} $out
chmod +w -R $out
printf "$(cat ${ideaPropertiesFile})\n\n# Default config\n\n$(cat "$out/${rootDir}/bin/idea.properties")" > "$out/${rootDir}/bin/idea.properties"
substituteInPlace "$out/${rootDir}/bin/${loName}" \
--replace-fail "${ide.vmoptsIDE}_VM_OPTIONS-'''" "${ide.vmoptsIDE}_VM_OPTIONS-'${vmoptsFile}'"
sed "s|${ide.outPath}|$out|" \
-i $(realpath $out/bin/${meta.mainProgram})
installPhase =
builtins.replaceStrings
[''--set-default JDK_HOME "$jdk" \'']
[
''
--set-default ${ideaPropertiesIDE}_PROPERTIES "${ideaPropertiesFile}" \
--set-default JDK_HOME "$jdk" \''
]
prevAttrs.installPhase;
});
ides = [
"clion"
"datagrip"
"dataspell"
"gateway"
"goland"
"idea-community"
"idea-ultimate"
"mps"
"phpstorm"
"pycharm-community"
"pycharm-professional"
"rider"
"ruby-mine"
"rust-rover"
"webstorm"
];
if test -f "$out/bin/${meta.mainProgram}-remote-dev-server"; then
sed "s|${ide.outPath}|$out|" \
-i $(realpath $out/bin/${meta.mainProgram}-remote-dev-server)
fi
'';
}
else ide;
in {
jetbrains =
prev.jetbrains
// (lib.listToAttrs (builtins.map (name: {
inherit name;
value = lib.makeOverridable (mkPatchedJetbrainsProductDerivation name) {};
})
ides));
supportedIdes));
})
]
7 changes: 4 additions & 3 deletions overlays/jetbrains/plugins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,23 +70,24 @@ in
})
ids);
in {
# Only use if you know what youre doing
# Only use if you know what you are doing
raw = {inherit files byId byName ids;};

addPlugins = ide: unprocessedPlugins: let
processPlugin = plugin:
if isDerivation plugin
then plugin
else if byId ? "${plugin}"
then byId."${plugin}" ide.pname ide.buildNumber
then byId."${plugin}" (ide.baseName or ide.pname) ide.buildNumber
else if byName ? "${plugin}"
then byName."${plugin}" ide.pname ide.buildNumber
then byName."${plugin}" (ide.baseName or ide.pname) ide.buildNumber
else throw "Could not resolve plugin ${plugin}";

plugins = map processPlugin unprocessedPlugins;
in
stdenv.mkDerivation rec {
pname = meta.mainProgram + "-with-plugins";
baseName = ide.baseName or meta.mainProgram;
version = ide.version;
src = ide;
dontInstall = true;
Expand Down

0 comments on commit d59fde4

Please sign in to comment.