Skip to content

Commit 4c82812

Browse files
committed
nix: remove old niv code paths & various fixes
* Use flake-compat so sources are same for stable/unstable users * completely remove niv * add envrc for direnv users * add retesteth to devshell, and unify flake devShell and shell.nix
1 parent 9178c64 commit 4c82812

File tree

13 files changed

+249
-267
lines changed

13 files changed

+249
-267
lines changed

.buildkite/default.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
import (import ../nix/sources.nix).nixkite
1+
import (import ../nix/compat.nix { src = ../.; }).defaultNix.inputs.nixkite

.buildkite/shell.nix

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{ sources, pkgs }:
1+
{ pkgs }:
22
let
33
# TODO, share this code with mantis build in this project
44
# sbt-protoc puts the scala plugin in /tmp/protobridge<some-random-number>.

.envrc

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
# shell gc root dir
3+
mkdir -p $(direnv_layout_dir)
4+
5+
# reload when these files change
6+
watch_file flake.nix
7+
watch_file flake.lock
8+
9+
# load the flake devShell
10+
eval "$(nix print-dev-env --profile $(direnv_layout_dir)/flake-profile)"
11+
} || use nix

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,8 @@ metals.sbt
2525
result
2626
.nix/
2727

28+
# direnv
29+
.direnv
30+
2831
# sonarScan
2932
.scannerwork/

default.nix

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
{ system ? builtins.currentSystem
2-
, src ? ./.
3-
, pkgs ? (import ./nix { inherit system src; }).pkgs
4-
}:
5-
pkgs.mantis
1+
let system = builtins.currentSystem;
2+
in
3+
(import ./nix/compat.nix { src = ./.; inherit system; }).defaultNix.defaultPackage.${system}

flake.lock

Lines changed: 17 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
inputs.nixpkgs.url =
66
"github:nixos/nixpkgs?rev=a98302aa9b9628915878a6ea9776c40a0bb02950";
77
inputs.sbt-derivation.url = "github:zaninime/sbt-derivation";
8+
inputs.nixkite.url = "github:johnae/nixkite";
9+
inputs.nixkite.flake = false;
810

9-
outputs = { self, nixpkgs, flake-utils, sbt-derivation
11+
outputs = { self, nixpkgs, flake-utils, sbt-derivation, ...
1012
}: # , libsonic, libsonic-jnr }:
1113
let
1214
overlay = import ./nix/overlay.nix self.rev;
@@ -44,7 +46,7 @@
4446
legacyPackages = pkgs;
4547

4648
defaultPackage = pkgs.mantis;
47-
devShell = pkgs.mkShell { nativeBuildInputs = with pkgs; [ solc sbt ]; };
49+
devShell = pkgs.devShell;
4850
apps.mantis = flake-utils.lib.mkApp { drv = pkgs.mantis; };
4951
defaultApp = apps.mantis;
5052
}) // (collectHydraSets

nix/compat.nix

Lines changed: 197 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,197 @@
1+
# Compatibility function to allow flakes to be used by
2+
# non-flake-enabled Nix versions. Given a source tree containing a
3+
# 'flake.nix' and 'flake.lock' file, it fetches the flake inputs and
4+
# calls the flake's 'outputs' function. It then returns an attrset
5+
# containing 'defaultNix' (to be used in 'default.nix'), 'shellNix'
6+
# (to be used in 'shell.nix').
7+
8+
{ src, system ? builtins.currentSystem or "unknown-system" }:
9+
10+
let
11+
12+
lockFilePath = src + "/flake.lock";
13+
14+
lockFile = builtins.fromJSON (builtins.readFile lockFilePath);
15+
16+
fetchTree =
17+
info:
18+
if info.type == "github" then
19+
{ outPath =
20+
fetchTarball
21+
({ url = "https://api.${info.host or "github.com"}/repos/${info.owner}/${info.repo}/tarball/${info.rev}"; }
22+
// (if info ? narHash then { sha256 = info.narHash; } else {})
23+
);
24+
rev = info.rev;
25+
shortRev = builtins.substring 0 7 info.rev;
26+
lastModified = info.lastModified;
27+
lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
28+
narHash = info.narHash;
29+
}
30+
else if info.type == "git" then
31+
{ outPath =
32+
builtins.fetchGit
33+
({ url = info.url; }
34+
// (if info ? rev then { inherit (info) rev; } else {})
35+
// (if info ? ref then { inherit (info) ref; } else {})
36+
);
37+
lastModified = info.lastModified;
38+
lastModifiedDate = formatSecondsSinceEpoch info.lastModified;
39+
narHash = info.narHash;
40+
} // (if info ? rev then {
41+
rev = info.rev;
42+
shortRev = builtins.substring 0 7 info.rev;
43+
} else {
44+
})
45+
else if info.type == "path" then
46+
{ outPath = builtins.path { path = info.path; };
47+
narHash = info.narHash;
48+
}
49+
else if info.type == "tarball" then
50+
{ outPath =
51+
fetchTarball
52+
({ inherit (info) url; }
53+
// (if info ? narHash then { sha256 = info.narHash; } else {})
54+
);
55+
}
56+
else if info.type == "gitlab" then
57+
{ inherit (info) rev narHash lastModified;
58+
outPath =
59+
fetchTarball
60+
({ url = "https://${info.host or "gitlab.com"}/api/v4/projects/${info.owner}%2F${info.repo}/repository/archive.tar.gz?sha=${info.rev}"; }
61+
// (if info ? narHash then { sha256 = info.narHash; } else {})
62+
);
63+
shortRev = builtins.substring 0 7 info.rev;
64+
}
65+
else
66+
# FIXME: add Mercurial, tarball inputs.
67+
throw "flake input has unsupported input type '${info.type}'";
68+
69+
callFlake4 = flakeSrc: locks:
70+
let
71+
flake = import (flakeSrc + "/flake.nix");
72+
73+
inputs = builtins.mapAttrs (n: v:
74+
if v.flake or true
75+
then callFlake4 (fetchTree (v.locked // v.info)) v.inputs
76+
else fetchTree (v.locked // v.info)) locks;
77+
78+
outputs = flakeSrc // (flake.outputs (inputs // {self = outputs;}));
79+
in
80+
assert flake.edition == 201909;
81+
outputs;
82+
83+
callLocklessFlake = flakeSrc:
84+
let
85+
flake = import (flakeSrc + "/flake.nix");
86+
outputs = flakeSrc // (flake.outputs ({ self = outputs; }));
87+
in outputs;
88+
89+
rootSrc = let
90+
# Try to clean the source tree by using fetchGit, if this source
91+
# tree is a valid git repository.
92+
tryFetchGit = src:
93+
if isGit && !isShallow
94+
then
95+
let res = builtins.fetchGit src;
96+
in if res.rev == "0000000000000000000000000000000000000000" then removeAttrs res ["rev" "shortRev"] else res
97+
else { outPath = src; };
98+
# NB git worktrees have a file for .git, so we don't check the type of .git
99+
isGit = builtins.pathExists (src + "/.git");
100+
isShallow = builtins.pathExists (src + "/.git/shallow");
101+
102+
in
103+
{ lastModified = 0; lastModifiedDate = formatSecondsSinceEpoch 0; }
104+
// (if src ? outPath then src else tryFetchGit src);
105+
106+
# Format number of seconds in the Unix epoch as %Y%m%d%H%M%S.
107+
formatSecondsSinceEpoch = t:
108+
let
109+
rem = x: y: x - x / y * y;
110+
days = t / 86400;
111+
secondsInDay = rem t 86400;
112+
hours = secondsInDay / 3600;
113+
minutes = (rem secondsInDay 3600) / 60;
114+
seconds = rem t 60;
115+
116+
# Courtesy of https://stackoverflow.com/a/32158604.
117+
z = days + 719468;
118+
era = (if z >= 0 then z else z - 146096) / 146097;
119+
doe = z - era * 146097;
120+
yoe = (doe - doe / 1460 + doe / 36524 - doe / 146096) / 365;
121+
y = yoe + era * 400;
122+
doy = doe - (365 * yoe + yoe / 4 - yoe / 100);
123+
mp = (5 * doy + 2) / 153;
124+
d = doy - (153 * mp + 2) / 5 + 1;
125+
m = mp + (if mp < 10 then 3 else -9);
126+
y' = y + (if m <= 2 then 1 else 0);
127+
128+
pad = s: if builtins.stringLength s < 2 then "0" + s else s;
129+
in "${toString y'}${pad (toString m)}${pad (toString d)}${pad (toString hours)}${pad (toString minutes)}${pad (toString seconds)}";
130+
131+
allNodes =
132+
builtins.mapAttrs
133+
(key: node:
134+
let
135+
sourceInfo =
136+
if key == lockFile.root
137+
then rootSrc
138+
else fetchTree (node.info or {} // removeAttrs node.locked ["dir"]);
139+
140+
subdir = if key == lockFile.root then "" else node.locked.dir or "";
141+
142+
flake = import (sourceInfo + (if subdir != "" then "/" else "") + subdir + "/flake.nix");
143+
144+
inputs = builtins.mapAttrs
145+
(inputName: inputSpec: allNodes.${resolveInput inputSpec})
146+
(node.inputs or {});
147+
148+
# Resolve a input spec into a node name. An input spec is
149+
# either a node name, or a 'follows' path from the root
150+
# node.
151+
resolveInput = inputSpec:
152+
if builtins.isList inputSpec
153+
then getInputByPath lockFile.root inputSpec
154+
else inputSpec;
155+
156+
# Follow an input path (e.g. ["dwarffs" "nixpkgs"]) from the
157+
# root node, returning the final node.
158+
getInputByPath = nodeName: path:
159+
if path == []
160+
then nodeName
161+
else
162+
getInputByPath
163+
# Since this could be a 'follows' input, call resolveInput.
164+
(resolveInput lockFile.nodes.${nodeName}.inputs.${builtins.head path})
165+
(builtins.tail path);
166+
167+
outputs = flake.outputs (inputs // { self = result; });
168+
169+
result = outputs // sourceInfo // { inherit inputs; inherit outputs; inherit sourceInfo; };
170+
in
171+
if node.flake or true then
172+
assert builtins.isFunction flake.outputs;
173+
result
174+
else
175+
sourceInfo
176+
)
177+
lockFile.nodes;
178+
179+
result =
180+
if !(builtins.pathExists lockFilePath)
181+
then callLocklessFlake rootSrc
182+
else if lockFile.version == 4
183+
then callFlake4 rootSrc (lockFile.inputs)
184+
else if lockFile.version >= 5 && lockFile.version <= 7
185+
then allNodes.${lockFile.root}
186+
else throw "lock file '${lockFilePath}' has unsupported version ${toString lockFile.version}";
187+
188+
in
189+
rec {
190+
defaultNix =
191+
result
192+
// (if result ? defaultPackage.${system} then { default = result.defaultPackage.${system}; } else {});
193+
194+
shellNix =
195+
defaultNix
196+
// (if result ? devShell.${system} then { default = result.devShell.${system}; } else {});
197+
}

nix/default.nix

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

nix/overlay.nix

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,4 +42,9 @@ rev: final: prev: {
4242

4343
retesteth = final.callPackage ./retesteth.nix { };
4444
lllc = final.callPackage ./lllc.nix { };
45+
46+
devShell = prev.mkShell {
47+
nativeBuildInputs = with prev; [ protobuf sbt final.retesteth ];
48+
inputsFrom = [ final.mantis ];
49+
};
4550
}

0 commit comments

Comments
 (0)