Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The nix command refers to the system wide nix instead of the locally included one #52

Closed
anka-213 opened this issue Oct 30, 2020 · 10 comments

Comments

@anka-213
Copy link

In default.nix, most references to programs are replaced, but not the nix executable. If that would refer to nixpkgs.nixFlakes, then you could use use flakes without having the beta version of nix installed globally.

Warning: Don't try --replace "nix" "${nix}/bin/nix" \ like did first, since that is bound to end badly.

@hmenke
Copy link

hmenke commented Dec 26, 2020

I have the exact opposite issue, namely that nix-direnv does not use the system Nix which could be nixFlakes but uses the nix attribute instead for nix-shell and nix-instantiate.

nix-direnv/default.nix

Lines 9 to 14 in fd48b56

postPatch = ''
substituteInPlace direnvrc \
--replace "grep" "${gnugrep}/bin/grep" \
--replace "nix-shell" "${nix}/bin/nix-shell" \
--replace "nix-instantiate" "${nix}/bin/nix-instantiate"
'';

I think there ought to be a switch.

@anka-213
Copy link
Author

anka-213 commented Dec 26, 2020

Yes, these are two sides of the same problem, namely that different versions of nix is used in different parts of the script, depending on if it is the nix binary or the nix-shell/nix-instantiate binaries.

And yes, a flag would be useful. In my local fork I've replaced nix with nixFlakes in the above lines.

@hmenke
Copy link

hmenke commented Dec 26, 2020

For my use-case it's just the other way around and nix has to be replaced by nixFlakes which can be done in an overlay. No need to fork for this little adjustment.

self: super: {
  nix-direnv = super.nix-direnv.overrideAttrs ({ postPatch, ... }: {
    postPatch = builtins.replaceStrings
      [ "${self.nix}" ] [ "${self.nixFlakes}" ] postPatch;
  });
}

For your use-case you could also use and overlay which would look like this:

self: super: {
  nix-direnv = super.nix-direnv.overrideAttrs ({ postPatch, ... }: {
    postPatch = postPatch + ''
      substituteInPlace direnvrc \
        --replace "nix print-dev-env" "${self.nixFlakes}/bin/nix print-dev-env"
    '';
  });
}

@Mic92
Copy link
Member

Mic92 commented Dec 28, 2020

For my use-case it's just the other way around and nix has to be replaced by nixFlakes which can be done in an overlay. No need to fork for this little adjustment.

self: super: {
  nix-direnv = super.nix-direnv.overrideAttrs ({ postPatch, ... }: {
    postPatch = builtins.replaceStrings
      [ "${self.nix}" ] [ "${self.nixFlakes}" ] postPatch;
  });
}

For your use-case you could also use and overlay which would look like this:

self: super: {
  nix-direnv = super.nix-direnv.overrideAttrs ({ postPatch, ... }: {
    postPatch = postPatch + ''
      substituteInPlace direnvrc \
        --replace "nix print-dev-env" "${self.nixFlakes}/bin/nix print-dev-env"
    '';
  });
}

Maybe direnvrc could be made more patch friendly, but I would prefer if it still works without the package. For use_flake one could also check if the current nix version supports flake and if not, try to download one nix version that has support.

@hmenke
Copy link

hmenke commented Dec 28, 2020

Actually this overlay was pretty stupid. It's much easier than that.

self: super: {
  nix-direnv = super.nix-direnv.override { nix = self.nixFlakes; };
}

To solve @anka-213's issue, may I suggest the following change to the expression in nixpkgs?

diff --git a/pkgs/tools/misc/nix-direnv/default.nix b/pkgs/tools/misc/nix-direnv/default.nix
index 392de7d1bd9..58b92e884cb 100644
--- a/pkgs/tools/misc/nix-direnv/default.nix
+++ b/pkgs/tools/misc/nix-direnv/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, gnugrep, nix }:
+{ lib, stdenv, fetchFromGitHub, gnugrep, nix, nixFlakes }:
 
 stdenv.mkDerivation rec {
   pname = "nix-direnv";
@@ -16,6 +16,7 @@ stdenv.mkDerivation rec {
   postPatch = ''
     substituteInPlace direnvrc \
       --replace "grep" "${gnugrep}/bin/grep" \
+      --replace "nix print-dev-env" "${nixFlakes}/bin/nix print-dev-env" \
       --replace "nix-shell" "${nix}/bin/nix-shell" \
       --replace "nix-instantiate" "${nix}/bin/nix-instantiate"
   '';

This can then still be overridden by

self: super: {
  nix-direnv = super.nix-direnv.override { nixFlakes = self.nix; };
}

@Mic92
Copy link
Member

Mic92 commented Jan 1, 2021

Actually this overlay was pretty stupid. It's much easier than that.

self: super: {
  nix-direnv = super.nix-direnv.override { nix = self.nixFlakes; };
}

To solve @anka-213's issue, may I suggest the following change to the expression in nixpkgs?

diff --git a/pkgs/tools/misc/nix-direnv/default.nix b/pkgs/tools/misc/nix-direnv/default.nix
index 392de7d1bd9..58b92e884cb 100644
--- a/pkgs/tools/misc/nix-direnv/default.nix
+++ b/pkgs/tools/misc/nix-direnv/default.nix
@@ -1,4 +1,4 @@
-{ lib, stdenv, fetchFromGitHub, gnugrep, nix }:
+{ lib, stdenv, fetchFromGitHub, gnugrep, nix, nixFlakes }:
 
 stdenv.mkDerivation rec {
   pname = "nix-direnv";
@@ -16,6 +16,7 @@ stdenv.mkDerivation rec {
   postPatch = ''
     substituteInPlace direnvrc \
       --replace "grep" "${gnugrep}/bin/grep" \
+      --replace "nix print-dev-env" "${nixFlakes}/bin/nix print-dev-env" \
       --replace "nix-shell" "${nix}/bin/nix-shell" \
       --replace "nix-instantiate" "${nix}/bin/nix-instantiate"
   '';

This can then still be overridden by

self: super: {
  nix-direnv = super.nix-direnv.override { nixFlakes = self.nix; };
}

At this point we might switch to nixFlakes for all versions? Because otherwise we would depend on both nix and nixFlakes.

@hmenke
Copy link

hmenke commented Jan 1, 2021

That would solve my use-case, I want to use nixFlakes for everything, but OP wants to use nix for the old-style nix-shell and nix-instantiate and nixFlakes for the new nix print-dev-env. Maybe something like this would work, but admittetly the usage is quite obscure:

diff --git a/pkgs/tools/misc/nix-direnv/default.nix b/pkgs/tools/misc/nix-direnv/default.nix
index 392de7d1bd9..46b7d3e803f 100644
--- a/pkgs/tools/misc/nix-direnv/default.nix
+++ b/pkgs/tools/misc/nix-direnv/default.nix
@@ -1,5 +1,11 @@
-{ lib, stdenv, fetchFromGitHub, gnugrep, nix }:
+{ lib, stdenv, fetchFromGitHub, gnugrep, nix, configure ? {} }:
 
+let
+  nixCmd = cmd:
+    if configure ? ${cmd}
+    then "${configure.${cmd}}/bin/${cmd}"
+    else "${nix}/bin/${cmd}";
+in
 stdenv.mkDerivation rec {
   pname = "nix-direnv";
   version = "1.2";
@@ -16,8 +22,9 @@ stdenv.mkDerivation rec {
   postPatch = ''
     substituteInPlace direnvrc \
       --replace "grep" "${gnugrep}/bin/grep" \
-      --replace "nix-shell" "${nix}/bin/nix-shell" \
-      --replace "nix-instantiate" "${nix}/bin/nix-instantiate"
+      --replace "nix print-dev-env" "${nixCmd "nix print-dev-env"}" \
+      --replace "nix-shell" "${nixCmd "nix-shell"}" \
+      --replace "nix-instantiate" "${nixCmd "nix-instantiate"}"
   '';
 
   installPhase = ''

and then

self: super: {
  nix-direnv = super.nix-direnv.override {
    configure = {
      "nix print-dev-env" = self.nixFlakes;
    };
  };
}

@hmenke
Copy link

hmenke commented Jan 1, 2021

BTW, nixos-shell has the same “problem”. If your system uses Flakes you have to install it with nixos-shell.override { nix = nixFlakes; }. The same holds for pretty much any other package that directly uses nix.

Another candidate with the same problem is zfs. You switch boot.zfsUnstable = true; on the system level which will give you the new kernel module and userspace tools, but there are several derivations that use zfs as an input.

Something like this would be addressed by Gentoo-like USE flags (see NixOS/nixpkgs#56227) but Eelco doesn't seem to like this at all.

@Mic92
Copy link
Member

Mic92 commented Jan 2, 2021

Fix in #59

@Mic92 Mic92 closed this as completed Jan 2, 2021
@Mic92
Copy link
Member

Mic92 commented Jan 2, 2021

BTW, nixos-shell has the same “problem”. If your system uses Flakes you have to install it with nixos-shell.override { nix = nixFlakes; }. The same holds for pretty much any other package that directly uses nix.

Another candidate with the same problem is zfs. You switch boot.zfsUnstable = true; on the system level which will give you the new kernel module and userspace tools, but there are several derivations that use zfs as an input.

Something like this would be addressed by Gentoo-like USE flags (see NixOS/nixpkgs#56227) but Eelco doesn't seem to like this at all.

There are plans to have module system for packages, which would be more powerful than USE flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants