如何覆盖 config.nix 中的 idea-ultimate 或 idea-community 所需的 jetbrains jdk 依赖项?

How can I override the jetbrains jdk dependency required by idea-ultimate or idea-community in config.nix?

我一直在按照 nixpkgs 手册 declarative package management 设置我的本地 nix 配置。

我想将 idea-ultimate 作为 myPackages 之一,但此时 idea 对 jetbrains jdk 的依赖是 broken,指向非-现有的 macOS 软件包。

正在尝试下载 jbrsdk-11_0_2-osx-x64-b485.1.tar.gz 而不是 jbrsdk-11_0_4-osx-x64-b485.1.tar.gz

我假设我可以通过如下覆盖 jetbrainsjdk 来解决这个问题,但我得到:error: attribute 'jetbrainsjdk' missing, at /Users/ldeck/.config/nixpkgs/config.nix:4:20 当我做任何类似 nix-env -qa ‘jetbrains.*’.

的事情时

重写 idea-ultimate 以使其使用固定 jdk 的正确方法是什么?

这是我的 ~./config/nixpkgs/config.nix

{
  allowUnfree = true;
  packageOverrides = pkgs: rec {
    jetbrainsjdk = pkgs.jetbrainsjdk.override {
      version = "520.11";
      src = pkgs.fetchurl {
        url = "https://bintray.com/jetbrains/intellij-jdk/download_file?file_path=jbrsdk-11_0_4-osx-x64-b${jetbrainsjdk.version}.tar.gz";
        sha256 = "0d1qwbssc8ih62rlfxxxcn8i65cjgycdfy1dc1b902j46dqjkq9z";
      };
    };


    myProfile = pkgs.writeText "my-profile" ''
      export PATH=$HOME/.nix-profile/bin:/nix/var/nix/profiles/default/bin:/sbin:/bin:/usr/sbin:/usr/bin
      export MANPATH=$HOME/.nix-profile/share/man:/nix/var/nix/profiles/default/share/man:/usr/share/man
    '';
    myPackages = with pkgs; buildEnv {
      name = "my-packages";
      paths = [
        (runCommand "profile" {} ''
          mkdir -p $out/etc/profile.d
          cp ${myProfile} $out/etc/profile.d/my-profile.sh
        '')
    aspell
    bc
    coreutils
    direnv
    emacs
    emscripten
    ffmpeg
    gdb
    git
    hello
    jq
    nixops
    nox
    scala
    silver-searcher
      ];
      pathsToLink = [ "/share/man" "/share/doc" "/bin" "/etc" "/Applications" ];
      extraOutputsToInstall = [ "man" "doc" ];
    };
  };
}


更新 1

感谢@ChrisStryczynski 建议我需要 with pkgs,我已经更进一步了。

但现在的问题是,当尝试使用自定义 jdk 安装 idea-ultimate 时,它​​仍然需要来自某处的损坏的、不存在的 jbrsdk-11_02-osx-x64-b485.1.tar.gz.drv

下面更新了配置和日志。

{
  allowUnfree = true;
  packageOverrides = pkgs: **with pkgs;** rec {

    myJetbrainsJdk = **pkgs.jetbrains.jdk.overrideAttrs** (oldAttrs: rec {
      version = "520.11";
      src = pkgs.fetchurl {
        url = "https://bintray.com/jetbrains/intellij-jdk/download_file?file_path=jbrsdk-11_0_4-osx-x64-b520.11.tar.gz";
        sha256 = "0d1qwbssc8ih62rlfxxxcn8i65cjgycdfy1dc1b902j46dqjkq9z";
      };
    });

    myIdeaUltimate = pkgs.jetbrains.idea-ultimate.override {
      jdk = myJetbrainsJdk;
    };

    ...

    myPackages = with pkgs; buildEnv {
    ...
    myIdeaUltimate
      ];
      ...
    };
  };
}

日志

nix-channel --update; nix-env -iA nixpkgs.myPackages
unpacking channels...
replacing old 'my-packages'
installing 'my-packages'
these derivations will be built:
  /nix/store/9kfi3k9q6hi7z3lwann318hndbah535v-idea-ultimate.desktop.drv
  /nix/store/ica1m5yq3f3y05xnw7ln1lnfvp0yjvyf-download_file?file_path=jbrsdk-11_0_4-osx-x64-b520.11.tar.gz.drv
  /nix/store/bf2hwhrvfl8g77gdiw053rayh06x0120-jetbrainsjdk-520.11.drv
  /nix/store/fazsa1a4l70s391rjk9yyi2hvrg0zbmp-download_file?file_path=jbrsdk-11_0_2-osx-x64-b485.1.tar.gz.drv
  /nix/store/fwwk976sd278zb68zy9wm5pkxss0rnhg-jetbrainsjdk-485.1.drv
  /nix/store/s3m2bpcyrnx9dcq4drh95882n0mk1d6m-ideaIU-2019.2.4-no-jbr.tar.gz.drv
  /nix/store/9kiajpmmsp3i6ysj4vdqq8dzi84mnr73-idea-ultimate-2019.2.4.drv
  /nix/store/jh1ixm54qinv8pk6kypvv6n6cfr4sws8-my-packages.drv
these paths will be fetched (0.02 MiB download, 0.12 MiB unpacked):
  /nix/store/hp90sbwznq1msv327f0lb27imvcvi80h-libnotify-0.7.8
building '/nix/store/9kfi3k9q6hi7z3lwann318hndbah535v-idea-ultimate.desktop.drv'...
copying path '/nix/store/hp90sbwznq1msv327f0lb27imvcvi80h-libnotify-0.7.8' from 'https://cache.nixos.org'...
building '/nix/store/fazsa1a4l70s391rjk9yyi2hvrg0zbmp-download_file?file_path=jbrsdk-11_0_2-osx-x64-b485.1.tar.gz.drv'...

trying https://bintray.com/jetbrains/intellij-jdk/download_file?file_path=jbrsdk-11_0_2-osx-x64-b485.1.tar.gz
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
curl: (22) The requested URL returned error: 404 Not Found
error: cannot download download_file?file_path=jbrsdk-11_0_2-osx-x64-b485.1.tar.gz from any mirror
builder for '/nix/store/fazsa1a4l70s391rjk9yyi2hvrg0zbmp-download_file?file_path=jbrsdk-11_0_2-osx-x64-b485.1.tar.gz.drv' failed with exit code 1
building '/nix/store/ica1m5yq3f3y05xnw7ln1lnfvp0yjvyf-download_file?file_path=jbrsdk-11_0_4-osx-x64-b520.11.tar.gz.drv'...
cannot build derivation '/nix/store/fwwk976sd278zb68zy9wm5pkxss0rnhg-jetbrainsjdk-485.1.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/9kiajpmmsp3i6ysj4vdqq8dzi84mnr73-idea-ultimate-2019.2.4.drv': 1 dependencies couldn't be built
cannot build derivation '/nix/store/jh1ixm54qinv8pk6kypvv6n6cfr4sws8-my-packages.drv': 1 dependencies couldn't be built
error: build of '/nix/store/jh1ixm54qinv8pk6kypvv6n6cfr4sws8-my-packages.drv' failed

而不是:

nix-env -iA nixpkgs.myPackages

就这样:

nix-env -iA nixpkgs.myIdeaUltimate

问题是:

  myPackages = with pkgs; buildEnv {
    ...
    myIdeaUltimate
      ];
      ...
    };

这里你仍然引用旧的pkgs.myIdeaUltimate

Nixos 似乎做了一些处理,将 pkgs 替换为 packageOverrides 中的适当内容。

感谢 How do you discover the override attributes for a derivation,已找到使用覆盖的解决方案。

# ~/config/nixpkgs/overlays/02-jetbrains.nix
self: super:
{
  jetbrains = super.jetbrains // {
    jdk = super.jetbrains.jdk.overrideAttrs (oldAttrs: rec {
      version = "520.11";
      src = super.fetchurl {
        url = "https://bintray.com/jetbrains/intellij-jbr/download_file?file_path=jbrsdk-11_0_4-osx-x64-b520.11.tar.gz";
    sha256 = "3fe1297133440a9056602d78d7987f9215139165bd7747b3303022a6f5e23834";
      };
      passthru = oldAttrs.passthru // {
        home = "${self.jetbrains.jdk}/Contents/Home";
      };
    });
    idea-ultimate = super.jetbrains.idea-ultimate.overrideAttrs (_: {
      name = "idea-ultimate.2019.2.4";
      src = super.fetchurl {
        url = "https://download.jetbrains.com/idea/ideaIU-2019.2.4-no-jbr.tar.gz";
    sha256 = "09mz4dx3zbnqw0vh4iqr8sn2s8mvgr7zvn4k7kqivsiv8f79g90a";
      };
    });
  };
}

安装:nix-env -iA 'nixpkgs.jetbrains.idea-ultimate'.

执行:idea-ultimate.

难题的关键部分是覆盖 passthru.home 变量以指向覆盖的 JDK 而不是 super 所需的变量。否则,您将下载旧的 JDK 用于运行时目的。

passthru = oldAttrs.passthru // {
  home = "${self.jetbrains.jdk}/Contents/Home”;
};

如果不附加 /Contents/Home,idea 将不会启动,因为 self.jetbrains.jdk 不是一个有效的家。