Difference between revisions of "How to fetch Nixpkgs with an empty NIX PATH"

From NixOS Wiki
Jump to: navigation, search
(Show how to simplify the recipe if one doesn't need to support pre-2.0 Nix)
m (rollback unauthorized mass edits)
Tag: Rollback
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
This recipe provides a way to fetch Nixpkgs with an empty <code>NIX_PATH</code>.  This
+
You can fetch Nixpkgs with an empty <code>NIX_PATH</code>.  This
 
comes in handy if you want to remove impure references to the <code>NIX_PATH</code> from
 
comes in handy if you want to remove impure references to the <code>NIX_PATH</code> from
your code base
+
your code base.
  
Save the following code to a <code>fetchNixpkgs.nix</code> file within your project:
+
To do so, you can use <code>builtins.fetchTarball</code>, like this:
 
 
<syntaxhighlight lang="nix">
 
{ rev                            # The Git revision of nixpkgs to fetch
 
, sha256                          # The SHA256 hash of the unpacked archive
 
, system ? builtins.currentSystem # This is overridable if necessary
 
}:
 
 
 
if (0 <= builtins.compareVersions builtins.nixVersion "1.12")
 
 
 
# In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
 
then (
 
  builtins.fetchTarball {
 
    url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
    inherit sha256;
 
  })
 
 
 
# This hack should at least work for Nix 1.11
 
else (
 
  (rec {
 
    tarball = import <nix/fetchurl.nix> {
 
      url    = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
      sha256 = null;
 
    };
 
 
 
    builtin-paths = import <nix/config.nix>;
 
     
 
    script = builtins.toFile "nixpkgs-unpacker" ''
 
      "$coreutils/mkdir" "$out"
 
      cd "$out"
 
      "$gzip" --decompress < "$tarball" | "$tar" -x --strip-components=1
 
    '';
 
 
 
    nixpkgs = builtins.derivation ({
 
      name = "nixpkgs-${builtins.substring 0 6 rev}";
 
 
 
      builder = builtins.storePath builtin-paths.shell;
 
 
 
      args = [ script ];
 
 
 
      inherit tarball system;
 
 
 
      tar      = builtins.storePath builtin-paths.tar;
 
      gzip      = builtins.storePath builtin-paths.gzip;
 
      coreutils = builtins.storePath builtin-paths.coreutils;
 
    } // (if null == sha256 then { } else {
 
      outputHashMode = "recursive";
 
      outputHashAlgo = "sha256";
 
      outputHash = sha256;
 
    }));
 
  }).nixpkgs)
 
</syntaxhighlight>
 
 
 
... and then you can use the saved file to fetch Nixpkgs like this:
 
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
 
let
 
let
   fetchNixpkgs = import ./fetchNixpkgs.nix;
+
   nixpkgs = builtins.fetchTarball {
 
+
     url   = "https://github.com/NixOS/nixpkgs/archive/3389f23412877913b9d22a58dfb241684653d7e9.tar.gz";
  nixpkgs = fetchNixpkgs {
 
     rev   = "3389f23412877913b9d22a58dfb241684653d7e9";
 
 
     sha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
 
     sha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
 
   };
 
   };
Line 73: Line 18:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
If you do not need to support Nix versions earlier than 2.0, then you can simplify <code>fetchNixpkgs.nix</code> to:
+
… replacing <code>3389f2…</code> with the desired revision of Nixpkgs and replacing <code>0wgm7s…</code> with the corresponding SHA256 hash.
  
<syntaxhighlight lang="nix">
+
You can use the following command to obtain the correct SHA256 hash to use if you prefer not to use trial and error:
{ rev    # The Git revision of nixpkgs to fetch
 
, sha256 # The SHA256 hash of the unpacked archive
 
}:
 
 
 
builtins.fetchTarball {
 
  url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
  inherit sha256;
 
}
 
</syntaxhighlight>
 
 
 
... or you can use <code>builtins.fetchTarball</code> directly instead of <code>fetchNixpkgs.nix</code>:
 
  
<syntaxhighlight lang="nix">
+
<syntaxhighlight lang="bash">
let
+
$ nix-prefetch-url --unpack "https://github.com/NixOS/nixpkgs/archive/${REVISION}.tar.gz"
  nixpkgs = builtins.fetchTarball {
 
    url   = "https://github.com/NixOS/nixpkgs/archive/3389f23412877913b9d22a58dfb241684653d7e9.tar.gz";
 
    sha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
 
  };
 
 
 
  pkgs = import nixpkgs { config = {}; };
 
 
 
in
 
  ...
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 
[[Category:Nix]] [[Category:Nixpkgs]] [[Category:Cookbook]]
 
[[Category:Nix]] [[Category:Nixpkgs]] [[Category:Cookbook]]

Latest revision as of 10:55, 6 April 2024

You can fetch Nixpkgs with an empty NIX_PATH. This comes in handy if you want to remove impure references to the NIX_PATH from your code base.

To do so, you can use builtins.fetchTarball, like this:

let
  nixpkgs = builtins.fetchTarball {
    url    = "https://github.com/NixOS/nixpkgs/archive/3389f23412877913b9d22a58dfb241684653d7e9.tar.gz";
    sha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
  };

  pkgs = import nixpkgs { config = {}; };

in
  ...

… replacing 3389f2… with the desired revision of Nixpkgs and replacing 0wgm7s… with the corresponding SHA256 hash.

You can use the following command to obtain the correct SHA256 hash to use if you prefer not to use trial and error:

$ nix-prefetch-url --unpack "https://github.com/NixOS/nixpkgs/archive/${REVISION}.tar.gz"