Skip to content

Commit 2ebd3ea

Browse files
authored
flake: remove, replace dependency management with npins (#3)
Pulling the flake inputs out for use in non-flake evaluations is a little hairy. We're already intentionally trying to avoid relying on the flake as much as possible, and the only reason it's there is so that flake users can override nixpkgs with the usual `follows` mechanism if they wanted. There's other ways to achieve the same thing without relying on `follows` though, so we don't even need flakes for that. This PR drops the flakes, and replaces the dependency management with [npins](https://github.com/andir/npins), which is much nicer to use from non-flake code.
1 parent b3afd45 commit 2ebd3ea

File tree

9 files changed

+188
-205
lines changed

9 files changed

+188
-205
lines changed

.github/workflows/deploy-docs.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ jobs:
1717
- name: Install Lix
1818
uses: samueldr/lix-gha-installer-action@v1
1919
- name: Fetch Elixir dependencies
20-
run: nix develop -c mix deps.get
20+
run: nix-shell --pure --run "mix deps.get"
2121
- name: Generate docs
22-
run: nix develop -c mix docs
22+
run: nix-shell --pure --run "mix docs"
2323
- name: Upload docs artifact
2424
uses: actions/upload-pages-artifact@v3
2525
with:

.github/workflows/test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ jobs:
1414
- name: Install Lix
1515
uses: samueldr/lix-gha-installer-action@v1
1616
- name: Run `mix deps.get` in a Nix shell
17-
run: nix develop -c mix deps.get
17+
run: nix-shell --pure --run "mix deps.get"
1818
- name: Run `mix test` in a Nix shell
19-
run: nix develop -c mix test
19+
run: nix-shell --pure --run "mix test"

flake.lock

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

flake.nix

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

nix/commit-hooks.nix

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
let
2-
inputs = import ./inputs.nix;
2+
pins = import ./npins;
33
in
44

55
{
6-
git-hooks ? inputs.git-hooks.lib,
6+
elixir,
7+
nixfmt-rfc-style,
8+
git-hooks ? import pins.git-hooks,
79
}:
810

911
git-hooks.run {
@@ -12,4 +14,8 @@ git-hooks.run {
1214
mix-format.enable = true;
1315
nixfmt-rfc-style.enable = true;
1416
};
17+
18+
tools = {
19+
inherit elixir nixfmt-rfc-style;
20+
};
1521
}

nix/inputs.nix

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

nix/npins/default.nix

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
This file is provided under the MIT licence:
3+
4+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
5+
6+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7+
8+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
9+
*/
10+
# Generated by npins. Do not modify; will be overwritten regularly
11+
let
12+
data = builtins.fromJSON (builtins.readFile ./sources.json);
13+
version = data.version;
14+
15+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/lists.nix#L295
16+
range =
17+
first: last: if first > last then [ ] else builtins.genList (n: first + n) (last - first + 1);
18+
19+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L257
20+
stringToCharacters = s: map (p: builtins.substring p 1 s) (range 0 (builtins.stringLength s - 1));
21+
22+
# https://github.com/NixOS/nixpkgs/blob/0258808f5744ca980b9a1f24fe0b1e6f0fecee9c/lib/strings.nix#L269
23+
stringAsChars = f: s: concatStrings (map f (stringToCharacters s));
24+
concatMapStrings = f: list: concatStrings (map f list);
25+
concatStrings = builtins.concatStringsSep "";
26+
27+
# If the environment variable NPINS_OVERRIDE_${name} is set, then use
28+
# the path directly as opposed to the fetched source.
29+
# (Taken from Niv for compatibility)
30+
mayOverride =
31+
name: path:
32+
let
33+
envVarName = "NPINS_OVERRIDE_${saneName}";
34+
saneName = stringAsChars (c: if (builtins.match "[a-zA-Z0-9]" c) == null then "_" else c) name;
35+
ersatz = builtins.getEnv envVarName;
36+
in
37+
if ersatz == "" then
38+
path
39+
else
40+
# this turns the string into an actual Nix path (for both absolute and
41+
# relative paths)
42+
builtins.trace "Overriding path of \"${name}\" with \"${ersatz}\" due to set \"${envVarName}\"" (
43+
if builtins.substring 0 1 ersatz == "/" then
44+
/. + ersatz
45+
else
46+
/. + builtins.getEnv "PWD" + "/${ersatz}"
47+
);
48+
49+
mkSource =
50+
name: spec:
51+
assert spec ? type;
52+
let
53+
path =
54+
if spec.type == "Git" then
55+
mkGitSource spec
56+
else if spec.type == "GitRelease" then
57+
mkGitSource spec
58+
else if spec.type == "PyPi" then
59+
mkPyPiSource spec
60+
else if spec.type == "Channel" then
61+
mkChannelSource spec
62+
else if spec.type == "Tarball" then
63+
mkTarballSource spec
64+
else
65+
builtins.throw "Unknown source type ${spec.type}";
66+
in
67+
spec // { outPath = mayOverride name path; };
68+
69+
mkGitSource =
70+
{
71+
repository,
72+
revision,
73+
url ? null,
74+
submodules,
75+
hash,
76+
branch ? null,
77+
...
78+
}:
79+
assert repository ? type;
80+
# At the moment, either it is a plain git repository (which has an url), or it is a GitHub/GitLab repository
81+
# In the latter case, there we will always be an url to the tarball
82+
if url != null && !submodules then
83+
builtins.fetchTarball {
84+
inherit url;
85+
sha256 = hash; # FIXME: check nix version & use SRI hashes
86+
}
87+
else
88+
let
89+
url =
90+
if repository.type == "Git" then
91+
repository.url
92+
else if repository.type == "GitHub" then
93+
"https://github.com/${repository.owner}/${repository.repo}.git"
94+
else if repository.type == "GitLab" then
95+
"${repository.server}/${repository.repo_path}.git"
96+
else
97+
throw "Unrecognized repository type ${repository.type}";
98+
urlToName =
99+
url: rev:
100+
let
101+
matched = builtins.match "^.*/([^/]*)(\\.git)?$" url;
102+
103+
short = builtins.substring 0 7 rev;
104+
105+
appendShort = if (builtins.match "[a-f0-9]*" rev) != null then "-${short}" else "";
106+
in
107+
"${if matched == null then "source" else builtins.head matched}${appendShort}";
108+
name = urlToName url revision;
109+
in
110+
builtins.fetchGit {
111+
rev = revision;
112+
inherit name;
113+
# hash = hash;
114+
inherit url submodules;
115+
};
116+
117+
mkPyPiSource =
118+
{ url, hash, ... }:
119+
builtins.fetchurl {
120+
inherit url;
121+
sha256 = hash;
122+
};
123+
124+
mkChannelSource =
125+
{ url, hash, ... }:
126+
builtins.fetchTarball {
127+
inherit url;
128+
sha256 = hash;
129+
};
130+
131+
mkTarballSource =
132+
{
133+
url,
134+
locked_url ? url,
135+
hash,
136+
...
137+
}:
138+
builtins.fetchTarball {
139+
url = locked_url;
140+
sha256 = hash;
141+
};
142+
in
143+
if version == 5 then
144+
builtins.mapAttrs mkSource data.pins
145+
else
146+
throw "Unsupported format version ${toString version} in sources.json. Try running `npins upgrade`"

0 commit comments

Comments
 (0)