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

From NixOS Wiki
Jump to: navigation, search
(One last whitespace change)
(emacs autoindent is now idempotent, 80-column limit respected)
Line 6: Line 6:
  
 
<syntaxhighlight lang="nix">
 
<syntaxhighlight lang="nix">
{ rev, sha256 }:
+
{ rev                             # The Git revision of nixpkgs to fetch
 +
, sha256                         # The SHA256 of the downloaded data
 +
, system ? builtins.currentSystem # This is overridable if necessary
 +
}:
  
if 0 <= builtins.compareVersions builtins.nixVersion "1.12"
+
with {
then
+
  ifThenElse = { bool, thenValue, elseValue }: (
 +
    if bool then thenValue else elseValue);
 +
};
  
builtins.fetchTarball {
+
ifThenElse {
   url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
+
   bool = (0 <= builtins.compareVersions builtins.nixVersion "1.12");
  inherit sha256;
 
}
 
  
else
+
  # In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
 +
  thenValue = (
 +
    builtins.fetchTarball {
 +
      url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 +
      inherit sha256;
 +
    });
  
# `builtins.fetchTarball` only accepts a `sha256` argument in Nix version 1.12 or later
+
  # This hack should at least work for Nix 1.11
with rec {
+
  elseValue = (
  system = builtins.currentSystem;
+
    (rec {
 +
      tarball = import <nix/fetchurl.nix> {
 +
        url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
 +
        inherit sha256;
 +
      };
  
  tarball = import <nix/fetchurl.nix> {
+
      builtin-paths = import <nix/config.nix>;
    url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
+
     
    inherit sha256;
+
      script = builtins.toFile "nixpkgs-unpacker" ''
  };
+
        "$coreutils/mkdir" "$out"
 +
        cd "$out"
 +
        "$gzip" --decompress < "$tarball" | "$tar" -x --strip-components=1
 +
      '';
  
  builtin-paths = import <nix/config.nix>;
+
      nixpkgs = builtins.derivation {
 +
        name = "nixpkgs-${builtins.substring 0 6 rev}";
  
  script = builtins.toFile "nixpkgs-unpacker" ''
+
        builder = builtins.storePath builtin-paths.shell;
    "$coreutils/mkdir" "$out"
 
    cd "$out"
 
    "$gzip" --decompress < "$tarball" | "$tar" --extract --strip-components=1
 
  '';
 
  
  nixpkgs = builtins.derivation {
+
        args = [ script ];
    name = "nixpkgs-${builtins.substring 0 6 rev}";
 
  
    builder = builtins.storePath builtin-paths.shell;
+
        inherit tarball system;
  
    args = [ script ];
+
        tar       = builtins.storePath builtin-paths.tar;
 
+
        gzip     = builtins.storePath builtin-paths.gzip;
    inherit tarball system;
+
        coreutils = builtins.storePath builtin-paths.coreutils;
 
+
      };
    tar = builtins.storePath builtin-paths.tar;
+
    }).nixpkgs);
    gzip = builtins.storePath builtin-paths.gzip;
+
}
    coreutils = builtins.storePath builtin-paths.coreutils;
 
  };
 
};
 
 
 
nixpkgs
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
Line 65: Line 71:
  
 
   pkgs = import nixpkgs { config = {}; };
 
   pkgs = import nixpkgs { config = {}; };
 
 
in
 
in
 
   ...
 
   ...

Revision as of 08:50, 14 November 2017

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
, system ? builtins.currentSystem # This is overridable if necessary
}:

with {
  ifThenElse = { bool, thenValue, elseValue }: (
    if bool then thenValue else elseValue);
};

ifThenElse {
  bool = (0 <= builtins.compareVersions builtins.nixVersion "1.12");

  # In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
  thenValue = (
    builtins.fetchTarball {
      url = "https://github.com/NixOS/nixpkgs/archive/${rev}.tar.gz";
      inherit sha256;
    });

  # This hack should at least work for Nix 1.11
  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" ''
        "$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;
      };
    }).nixpkgs);
}

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

let
  fetchNixpkgs = import ./fetchNixpkgs.nix;

  nixpkgs = fetchNixpkgs {
     rev = "76d649b59484607901f0c1b8f737d8376a904019";
     sha256 = "01c2f4mj4ahir0sxk9kxbymg2pki1pc9a3y6r9x6ridry75fzb8h";
  };

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