-
Notifications
You must be signed in to change notification settings - Fork 0
/
flake.nix
102 lines (92 loc) · 3.08 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
{
inputs.nixpkgs.url = "github:NixOS/nixpkgs/release-24.05";
outputs = { nixpkgs, self } :
let wantedAttrs = ["packages" "checks" "devShells"];
in {
# 'withCaches is meant to wrap the entire 'outputs'. It
# allows any 'checks'/'packages' to have an extra argument,
# the cache. E.g.:
#
# {
# inputs.nixpkgs.url = "..."
# inputs.incrementalize.url = "github:garnix-io/incrementalize";
# outputs = { nixpkgs, ...} : incrementalize.lib.withCaches {
# packages.x86_64.default = cache: ...;
# };
# }
#
# If referencing one another the outputs should use 'self', since there
# the output will not be a function.
lib.withCaches = self.lib.withCachesFor {};
lib.withCachesFor = prev: outputs:
let emptyDir = "${./emptyDir}";
mapAttrsIfSet = fn : s : if builtins.isAttrs s then builtins.mapAttrs fn s else s;
in (mapAttrsIfSet (type:
mapAttrsIfSet (sys:
mapAttrsIfSet (pkg: def:
if builtins.elem type wantedAttrs && builtins.isFunction def
then (def (prev.${type}.${sys}.${pkg}.intermediates or emptyDir))
else def
)))) outputs;
# nix-unit tests
tests = {
testAppliesFunctionArguments = {
description = ''
Package arguments that are functions should be applied to the cache
'';
expr =
let flake = self.lib.withCaches {
packages.x86_64-linux.foo = cache : cache;
};
in builtins.isString flake.packages.x86_64-linux.foo;
expected = true;
};
testDoesNothingWhenNotFunction = {
description = ''
Package arguments that are not functions should be applied returned unmodified
'';
expr =
let flake = self.lib.withCaches {
packages.x86_64-linux.foo = 1781;
};
in flake.packages.x86_64-linux.foo;
expected = 1781;
};
testHandlesWeirdOuputs = {
description = ''
Package arguments that are not functions should be applied returned unmodified
'';
expr =
self.lib.withCaches {
foo = 3;
bar.baz.quux.wat.skibidi = 4;
};
expected = {
foo = 3;
bar.baz.quux.wat.skibidi = 4;
};
};
};
checks.x86_64-linux.unitTests =
let pkgs = nixpkgs.legacyPackages.x86_64-linux;
in pkgs.runCommand "unitTests"
{
nativeBuildInputs = [ pkgs.nix-unit ];
} ''
export HOME="$(realpath .)"
# The nix derivation must be able to find all used inputs in the nix-store because it cannot download it during buildTime.
${pkgs.nix-unit}/bin/nix-unit --eval-store "$HOME" \
--extra-experimental-features flakes \
--override-input nixpkgs ${nixpkgs} \
--flake ${self}#tests
touch $out
'';
devShells.x86_64-linux.default =
let pkgs = nixpkgs.legacyPackages.x86_64-linux;
in pkgs.mkShell {
packages = [
pkgs.nix-unit
];
};
};
}