]> git.sesse.net Git - bcachefs-tools-debian/blob - nix/fetchnix.nix
2f98788ff9b9434e5e516a915c09f8f8058108e9
[bcachefs-tools-debian] / nix / fetchnix.nix
1 # `builtins.fetchTarball` only accepts a `sha256` argument in Nix version 1.12
2 # or later, so here we provide a function that can provide a compatible interface
3 # to Nix 1.11 or Nix 1.12
4 #
5 # TODO FIXME: remove this sometime after Nix 1.12 goes stable
6
7 { url                             # URL of the nixpkgs tarball to download
8 , rev                             # The Git revision of nixpkgs to fetch
9 , sha256                          # The SHA256 of the downloaded data
10 , system ? builtins.currentSystem # This is overridable if necessary
11 }:
12
13 with {
14   ifThenElse = { bool, thenValue, elseValue }: (
15     if bool then thenValue else elseValue);
16 };
17
18 ifThenElse {
19   bool = (0 <= builtins.compareVersions builtins.nixVersion "1.12");
20
21   # In Nix 1.12, we can just give a `sha256` to `builtins.fetchTarball`.
22   thenValue = (builtins.fetchTarball { inherit url sha256; });
23
24   # This hack should at least work for Nix 1.11
25   elseValue = (
26     (rec {
27       tarball = import <nix/fetchurl.nix> { inherit url sha256; };
28       builtin-paths = import <nix/config.nix>;
29
30       script = builtins.toFile "nixpkgs-unpacker" ''
31         "$coreutils/mkdir" "$out"
32         cd "$out"
33         "$gzip" --decompress < "$tarball" | "$tar" -x --strip-components=1
34       '';
35
36       nixpkgs = builtins.derivation {
37         name = "nixpkgs-${builtins.substring 0 6 rev}";
38
39         builder = builtins.storePath builtin-paths.shell;
40         args = [ script ];
41
42         inherit tarball system;
43         tar       = builtins.storePath builtin-paths.tar;
44         gzip      = builtins.storePath builtin-paths.gzip;
45         coreutils = builtins.storePath builtin-paths.coreutils;
46       };
47     }).nixpkgs);
48 }