Skip to content

Commit b8d851f

Browse files
committed
Add niv sources at haskell-nix.sources
I think it would be nice to be able to use niv to manage sources. I like `niv update` to bump to latest version or switch branches. It is easy enough to modify the `nix/sources.json` manually if anyone does not want to learn a new tool and since @michaelpj already added a niv like `sources` very little changes. I have exposed the `sources` as `haskell-nix.sources` so that we can get at it from wherever we need it. I've just put the `nixpkgs` pins in `nix/sources.json` for now and we can add things too it as we need to update them.
1 parent 78bc725 commit b8d851f

9 files changed

+198
-48
lines changed

default.nix

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
let haskellNix = rec {
2-
sources = {
3-
inherit (import ./nixpkgs/default.nix) nixpkgs-1909 nixpkgs-2003 nixpkgs-default;
4-
};
1+
let haskellNix = {
2+
sourcesOverride ? {},
3+
... }: rec {
4+
sources = (import ./nix/sources.nix) // sourcesOverride;
55

66
config = import ./config.nix;
77
overlays = [ allOverlays.combined ];
@@ -10,17 +10,17 @@ let haskellNix = rec {
1010
pkgs = import sources.nixpkgs-default nixpkgsArgs;
1111
};
1212

13-
haskellNixV1 = haskellNix.nixpkgsArgs;
13+
haskellNixV1 = (haskellNix {}).nixpkgsArgs;
1414
haskellNixV2 = haskellNix;
1515

1616
v1DeprecationMessage = "Version 1 is deprecated: use version 2 (nixpkgs arguments are available as the `nixpkgsArgs` attribute of version 2)";
1717
# If no arguments, then you get V1
1818
# I'd like to make importing directly issue a warning, but I couldn't figure out a way to make it happen
1919
in haskellNixV1 // {
20-
__functor = _: { version ? 2 }:
20+
__functor = _: { version ? 2, ... }@args:
2121
if version == 1
2222
then builtins.trace v1DeprecationMessage haskellNixV1
2323
else if version == 2
24-
then haskellNixV2
24+
then haskellNixV2 args
2525
else builtins.throw ("haskell.nix: unknown version: " + (builtins.toString version));
2626
}

nix/sources.json

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"niv": {
3+
"branch": "master",
4+
"description": "Easy dependency management for Nix projects",
5+
"homepage": "https://github.com/nmattia/niv",
6+
"owner": "nmattia",
7+
"repo": "niv",
8+
"rev": "f73bf8d584148677b01859677a63191c31911eae",
9+
"sha256": "0jlmrx633jvqrqlyhlzpvdrnim128gc81q5psz2lpp2af8p8q9qs",
10+
"type": "tarball",
11+
"url": "https://github.com/nmattia/niv/archive/f73bf8d584148677b01859677a63191c31911eae.tar.gz",
12+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
13+
},
14+
"nixpkgs-1909": {
15+
"branch": "nixpkgs-19.09-darwin",
16+
"description": "Nix Packages collection",
17+
"homepage": null,
18+
"owner": "NixOS",
19+
"repo": "nixpkgs",
20+
"rev": "9237a09d8edbae9951a67e9a3434a07ef94035b7",
21+
"sha256": "05bizymljzzd665bpsjbhxamcgzq7bkjjzjfapkl2nicy774ak4x",
22+
"type": "tarball",
23+
"url": "https://github.com/NixOS/nixpkgs/archive/9237a09d8edbae9951a67e9a3434a07ef94035b7.tar.gz",
24+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
25+
},
26+
"nixpkgs-2003": {
27+
"branch": "nixpkgs-20.03-darwin",
28+
"description": "Nix Packages collection",
29+
"homepage": null,
30+
"owner": "NixOS",
31+
"repo": "nixpkgs",
32+
"rev": "cac363c661817666e43d047addfaa722610d425f",
33+
"sha256": "0fi8hgddy8qh2jrsa40jw7jxnr5lrhq2ji6a2xbndllivhzc31kf",
34+
"type": "tarball",
35+
"url": "https://github.com/NixOS/nixpkgs/archive/cac363c661817666e43d047addfaa722610d425f.tar.gz",
36+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
37+
},
38+
"nixpkgs-default": {
39+
"branch": "nixpkgs-20.03-darwin",
40+
"description": "Nix Packages collection",
41+
"homepage": null,
42+
"owner": "NixOS",
43+
"repo": "nixpkgs",
44+
"rev": "cac363c661817666e43d047addfaa722610d425f",
45+
"sha256": "0fi8hgddy8qh2jrsa40jw7jxnr5lrhq2ji6a2xbndllivhzc31kf",
46+
"type": "tarball",
47+
"url": "https://github.com/NixOS/nixpkgs/archive/cac363c661817666e43d047addfaa722610d425f.tar.gz",
48+
"url_template": "https://github.com/<owner>/<repo>/archive/<rev>.tar.gz"
49+
}
50+
}

nix/sources.nix

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# This file has been generated by Niv.
2+
3+
let
4+
5+
#
6+
# The fetchers. fetch_<type> fetches specs of type <type>.
7+
#
8+
9+
fetch_file = pkgs: spec:
10+
if spec.builtin or true then
11+
builtins_fetchurl { inherit (spec) url sha256; }
12+
else
13+
pkgs.fetchurl { inherit (spec) url sha256; };
14+
15+
fetch_tarball = pkgs: spec:
16+
if spec.builtin or true then
17+
builtins_fetchTarball { inherit (spec) url sha256; }
18+
else
19+
pkgs.fetchzip { inherit (spec) url sha256; };
20+
21+
fetch_git = spec:
22+
builtins.fetchGit { url = spec.repo; inherit (spec) rev ref; };
23+
24+
fetch_builtin-tarball = spec:
25+
builtins.trace
26+
''
27+
WARNING:
28+
The niv type "builtin-tarball" will soon be deprecated. You should
29+
instead use `builtin = true`.
30+
31+
$ niv modify <package> -a type=tarball -a builtin=true
32+
''
33+
builtins_fetchTarball { inherit (spec) url sha256; };
34+
35+
fetch_builtin-url = spec:
36+
builtins.trace
37+
''
38+
WARNING:
39+
The niv type "builtin-url" will soon be deprecated. You should
40+
instead use `builtin = true`.
41+
42+
$ niv modify <package> -a type=file -a builtin=true
43+
''
44+
(builtins_fetchurl { inherit (spec) url sha256; });
45+
46+
#
47+
# Various helpers
48+
#
49+
50+
# The set of packages used when specs are fetched using non-builtins.
51+
mkPkgs = sources:
52+
let
53+
sourcesNixpkgs =
54+
import (builtins_fetchTarball { inherit (sources.nixpkgs) url sha256; }) {};
55+
hasNixpkgsPath = builtins.any (x: x.prefix == "nixpkgs") builtins.nixPath;
56+
hasThisAsNixpkgsPath = <nixpkgs> == ./.;
57+
in
58+
if builtins.hasAttr "nixpkgs" sources
59+
then sourcesNixpkgs
60+
else if hasNixpkgsPath && ! hasThisAsNixpkgsPath then
61+
import <nixpkgs> {}
62+
else
63+
abort
64+
''
65+
Please specify either <nixpkgs> (through -I or NIX_PATH=nixpkgs=...) or
66+
add a package called "nixpkgs" to your sources.json.
67+
'';
68+
69+
# The actual fetching function.
70+
fetch = pkgs: name: spec:
71+
72+
if ! builtins.hasAttr "type" spec then
73+
abort "ERROR: niv spec ${name} does not have a 'type' attribute"
74+
else if spec.type == "file" then fetch_file pkgs spec
75+
else if spec.type == "tarball" then fetch_tarball pkgs spec
76+
else if spec.type == "git" then fetch_git spec
77+
else if spec.type == "builtin-tarball" then fetch_builtin-tarball spec
78+
else if spec.type == "builtin-url" then fetch_builtin-url spec
79+
else
80+
abort "ERROR: niv spec ${name} has unknown type ${builtins.toJSON spec.type}";
81+
82+
# Ports of functions for older nix versions
83+
84+
# a Nix version of mapAttrs if the built-in doesn't exist
85+
mapAttrs = builtins.mapAttrs or (
86+
f: set: with builtins;
87+
listToAttrs (map (attr: { name = attr; value = f attr set.${attr}; }) (attrNames set))
88+
);
89+
90+
# fetchTarball version that is compatible between all the versions of Nix
91+
builtins_fetchTarball = { url, sha256 }@attrs:
92+
let
93+
inherit (builtins) lessThan nixVersion fetchTarball;
94+
in
95+
if lessThan nixVersion "1.12" then
96+
fetchTarball { inherit url; }
97+
else
98+
fetchTarball attrs;
99+
100+
# fetchurl version that is compatible between all the versions of Nix
101+
builtins_fetchurl = { url, sha256 }@attrs:
102+
let
103+
inherit (builtins) lessThan nixVersion fetchurl;
104+
in
105+
if lessThan nixVersion "1.12" then
106+
fetchurl { inherit url; }
107+
else
108+
fetchurl attrs;
109+
110+
# Create the final "sources" from the config
111+
mkSources = config:
112+
mapAttrs (
113+
name: spec:
114+
if builtins.hasAttr "outPath" spec
115+
then abort
116+
"The values in sources.json should not have an 'outPath' attribute"
117+
else
118+
spec // { outPath = fetch config.pkgs name spec; }
119+
) config.sources;
120+
121+
# The "config" used by the fetchers
122+
mkConfig =
123+
{ sourcesFile ? ./sources.json
124+
, sources ? builtins.fromJSON (builtins.readFile sourcesFile)
125+
, pkgs ? mkPkgs sources
126+
}: rec {
127+
# The sources, i.e. the attribute set of spec name to spec
128+
inherit sources;
129+
130+
# The "pkgs" (evaluated nixpkgs) to use for e.g. non-builtin fetchers
131+
inherit pkgs;
132+
};
133+
in
134+
mkSources (mkConfig {}) // { __functor = _: settings: mkSources (mkConfig settings); }

nixpkgs/default.nix

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,4 @@
1-
# see ../docs/dev/nixpkgs-pin.md
2-
let
3-
fetch = jsonFile:
4-
with builtins;
5-
let spec = fromJSON (readFile jsonFile);
6-
in fetchTarball {
7-
name = "nixpkgs";
8-
inherit (spec) sha256;
9-
url = "${spec.url}/archive/${spec.rev}.tar.gz";
10-
};
11-
in
1+
# This file is for backwards compatibility only
122
{
13-
nixpkgs-2003 = fetch (./. + "/release-20.03.json");
14-
nixpkgs-1909 = fetch (./. + "/release-19.09.json");
15-
nixpkgs-default = fetch (./. + "/github.json");
3+
inherit (import ../nix/sources.nix) nixpkgs-2003 nixpkgs-1909 nixpkgs-default;
164
}

nixpkgs/github.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

nixpkgs/hackage-src.json

Whitespace-only changes.

nixpkgs/release-19.09.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

nixpkgs/release-20.03.json

Lines changed: 0 additions & 9 deletions
This file was deleted.

overlays/haskell.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ in {
2929
# overlays.
3030
defaultModules = [];
3131

32+
# Niv based source pins. See https://github.com/nmattia/niv#niv
33+
# for details on how to update this using the niv tool
34+
# or edit nix/sources.json manually if you prefer.
35+
sources = import ../nix/sources.nix;
36+
3237
# We provide a `callPackage` function to consumers for
3338
# convenience. We will however refrain from using it
3439
# here and be explicit about imports and dependencies.

0 commit comments

Comments
 (0)