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

From NixOS Wiki
Jump to: navigation, search
(emacs autoindent is now idempotent, 80-column limit respected)
(Make fetchNixpkgs use a fixed-output nixpkgs derivation and use the outputSha256 hash for builtins.fetchTarball; additionally, use if then else instead)
Line 8: Line 8:
 
{ rev                            # The Git revision of nixpkgs to fetch
 
{ rev                            # The Git revision of nixpkgs to fetch
 
, sha256                          # The SHA256 of the downloaded data
 
, sha256                          # The SHA256 of the downloaded data
 +
, outputSha256 ? null            # The SHA256 fixed-output hash
 
, system ? builtins.currentSystem # This is overridable if necessary
 
, system ? builtins.currentSystem # This is overridable if necessary
 
}:
 
}:
  
with {
+
if (0 <= builtins.compareVersions builtins.nixVersion "1.12")
  ifThenElse = { bool, thenValue, elseValue }: (
 
    if bool then thenValue else elseValue);
 
};
 
  
ifThenElse {
+
# In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
  bool = (0 <= builtins.compareVersions builtins.nixVersion "1.12");
+
then (
 +
  builtins.fetchTarball {
 +
    url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 +
    sha256 = outputSha256;
 +
  })
  
  # In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
+
# This hack should at least work for Nix 1.11
   thenValue = (
+
else (
     builtins.fetchTarball {
+
   (rec {
 +
     tarball = import <nix/fetchurl.nix> {
 
       url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
       url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
       inherit sha256;
 
       inherit sha256;
     });
+
     };
  
  # This hack should at least work for Nix 1.11
+
     builtin-paths = import <nix/config.nix>;
  elseValue = (
 
     (rec {
 
      tarball = import <nix/fetchurl.nix> {
 
        url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 
        inherit sha256;
 
      };
 
 
 
      builtin-paths = import <nix/config.nix>;
 
 
        
 
        
      script = builtins.toFile "nixpkgs-unpacker" ''
+
    script = builtins.toFile "nixpkgs-unpacker" ''
        "$coreutils/mkdir" "$out"
+
      "$coreutils/mkdir" "$out"
        cd "$out"
+
      cd "$out"
        "$gzip" --decompress < "$tarball" | "$tar" -x --strip-components=1
+
      "$gzip" --decompress < "$tarball" | "$tar" -x --strip-components=1
      '';
+
    '';
  
      nixpkgs = builtins.derivation {
+
    nixpkgs = builtins.derivation ({
        name = "nixpkgs-${builtins.substring 0 6 rev}";
+
      name = "nixpkgs-${builtins.substring 0 6 rev}";
  
        builder = builtins.storePath builtin-paths.shell;
+
      builder = builtins.storePath builtin-paths.shell;
  
        args = [ script ];
+
      args = [ script ];
  
        inherit tarball system;
+
      inherit tarball system;
  
        tar      = builtins.storePath builtin-paths.tar;
+
      tar      = builtins.storePath builtin-paths.tar;
        gzip      = builtins.storePath builtin-paths.gzip;
+
      gzip      = builtins.storePath builtin-paths.gzip;
        coreutils = builtins.storePath builtin-paths.coreutils;
+
      coreutils = builtins.storePath builtin-paths.coreutils;
       };
+
    } // (if null == outputSha256 then { } else {
     }).nixpkgs);
+
      outputHashMode = "recursive";
}
+
       outputHashAlgo = "sha256";
 +
      outputHash = outputSha256;
 +
     }));
 +
  }).nixpkgs)
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 66: Line 64:
  
 
   nixpkgs = fetchNixpkgs {
 
   nixpkgs = fetchNixpkgs {
     rev = "76d649b59484607901f0c1b8f737d8376a904019";
+
     rev         = "3389f23412877913b9d22a58dfb241684653d7e9";
     sha256 = "01c2f4mj4ahir0sxk9kxbymg2pki1pc9a3y6r9x6ridry75fzb8h";
+
     sha256       = "1zf05a90d29bpl7j56y20r3kmrl4xkvg7gsfi55n6bb2r0xp2ma5";
 +
    outputSha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
 
   };
 
   };
  

Revision as of 17:53, 29 March 2018

This recipe provides a way to 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

Save the following code to a fetchNixpkgs.nix file within your project:

{ rev                             # The Git revision of nixpkgs to fetch
, sha256                          # The SHA256 of the downloaded data
, outputSha256 ? null             # The SHA256 fixed-output hash
, 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";
    sha256 = outputSha256;
  })

# 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";
      inherit sha256;
    };

    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 == outputSha256 then { } else {
      outputHashMode = "recursive";
      outputHashAlgo = "sha256";
      outputHash = outputSha256;
    }));
  }).nixpkgs)

... and then you can use the saved file to fetch Nixpkgs like this:

let
  fetchNixpkgs = import ./fetchNixpkgs.nix;

  nixpkgs = fetchNixpkgs {
     rev          = "3389f23412877913b9d22a58dfb241684653d7e9";
     sha256       = "1zf05a90d29bpl7j56y20r3kmrl4xkvg7gsfi55n6bb2r0xp2ma5";
     outputSha256 = "0wgm7sk9fca38a50hrsqwz6q79z35gqgb9nw80xz7pfdr4jy9pf8";
  };

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