Repository navigation
Expand file tree
/
Copy pathflake.nix
More file actions
203 lines (181 loc) · 6.95 KB
/
Copy pathflake.nix
File metadata and controls
203 lines (181 loc) · 6.95 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
{
inputs = {
cache-server = {
flake = false;
url = "github:icedos/cache-server/cache";
};
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs =
{
nixpkgs,
self,
...
}:
{
lib.mkIceDOS =
{
configRoot,
stateDir ? ".state",
}:
let
inherit (builtins) isString pathExists;
isFlake = value: (value._type or null) == "flake";
# config.toml is optional; flake.nix is what marks the config root.
_configRoot =
if ((isFlake configRoot) && (pathExists "${configRoot}/flake.nix")) then
configRoot
else
(throw "The value of `configRoot` is invalid. Please set `configRoot = self;`.");
_stateDir =
if (isString stateDir) then stateDir else (throw "The value of `stateDir` should be a string.");
inherit (import ./lib/config/load-user-config.nix _configRoot) icedos;
system = icedos.system.arch or "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
inherit (pkgs) lib writeShellScript;
inherit (lib) makeBinPath;
icedosBuild = toString (
writeShellScript "icedos-build" ''
set -e
export PATH="${
with pkgs;
makeBinPath [
git
jsonfmt
nh
nix
nixfmt
python3
]
}:$PATH"
export ICEDOS_ROOT="${self}"
export ICEDOS_CONFIG_ROOT="$PWD"
export ICEDOS_STATE_DIR="$PWD/${_stateDir}"
export ICEDOS_INPUTS_PREFIX="${(import ./lib/constants.nix { }).INPUTS_PREFIX}"
export ICEDOS_GITHUB_TOKEN_PATH="''${ICEDOS_GITHUB_TOKEN_PATH:-${
icedos.system.githubTokenPath or (import ./lib/constants.nix { }).GITHUB_TOKEN_PATH
}}"
export ICEDOS_GITHUB_TOKEN="''${ICEDOS_GITHUB_TOKEN:-${icedos.system.githubToken or ""}}"
mkdir -p "$ICEDOS_STATE_DIR"
[ -f "$ICEDOS_STATE_DIR/build.sh" ] && rm "$ICEDOS_STATE_DIR/build.sh"
echo "#!/usr/bin/env bash" >>"$ICEDOS_STATE_DIR/build.sh"
echo "set -e" >>"$ICEDOS_STATE_DIR/build.sh"
echo "cd \"$PWD\"" >>"$ICEDOS_STATE_DIR/build.sh"
echo "nix run path:. -- \"\$@\"" >>"$ICEDOS_STATE_DIR/build.sh"
export PYTHONPATH="${self}''${PYTHONPATH:+:$PYTHONPATH}"
# -P keeps cwd off sys.path, so a stray build/ dir can't shadow the package.
python3 -P -m build "$@"
''
);
in
{
apps.${system}.default = {
type = "app";
program = icedosBuild;
};
devShells.${system}.default = pkgs.mkShell {
packages = [ pkgs.nix ];
shellHook = ''
source ${self}/lib/prelude.sh
icedos() {
if [ "$1" != "rebuild" ]; then
echo "Available commands:"
echo -e "> ''${PURPLE}rebuild''${NC} rebuild the system"
return 1
fi
shift
local dir=""
local args=()
while [[ $# -gt 0 ]]; do
case "$1" in
--dir)
[[ $# -ge 2 ]] || { echo "--dir requires a directory" >&2; return 1; }
dir="$2"
shift 2
;;
*) args+=("$1"); shift ;;
esac
done
if [ -n "$dir" ]; then
cd "$dir" || return 1
fi
# Fail before `nix run path:.` copies the whole dir to the store.
if [[ ! -f flake.nix || ! "$(<flake.nix)" =~ \.lib\.mkIceDOS ]]; then
echo "no IceDOS config flake.nix (calling mkIceDOS) in $PWD" >&2
return 1
fi
nix run path:. -- "''${args[@]}"
}
export -f icedos
'';
};
};
# Eval-only lib tests (`tests/tests.nix`), the `build/` unit tests, and a
# formatting gate, all as flake checks.
checks =
let
inherit (nixpkgs) lib;
# Source checks run on one system, not every exposed one: nixfmt is a
# Haskell build that cannot bootstrap on some (`armv6l` GHC).
sourceCheckSystem = "x86_64-linux";
in
lib.genAttrs lib.systems.flakeExposed (
system:
let
pkgs = nixpkgs.legacyPackages.${system};
# tryEval each result so a test that throws lands in the readable
# failure list instead of aborting `nix flake check` with an eval trace.
results = lib.mapAttrs (
name: value:
let
r = builtins.tryEval value;
in
if r.success then r.value else "FAIL: ${name} threw during evaluation"
) (import ./tests/tests.nix { inherit (pkgs) lib; });
failures = lib.filterAttrs (_: value: value != "ok") results;
in
{
lib-tests = pkgs.runCommand "icedos-lib-tests" { } (
if failures == { } then
"echo 'lib tests: all ok'\ntouch $out"
else
lib.concatStringsSep "\n" (
[
"echo 'lib tests FAILED:' >&2"
]
++ (lib.mapAttrsToList (
name: value: "echo ${lib.escapeShellArg " ${name}: ${value}"} >&2"
) failures)
++ [ "exit 1" ]
)
);
}
// lib.optionalAttrs (system == sourceCheckSystem) {
# `build/` is pure-python; arg parsing, lock reading, token precedence,
# hooks, snapshots and the phase order are all testable without a build.
python-tests =
pkgs.runCommand "icedos-python-tests"
{
nativeBuildInputs = [ pkgs.python3 ];
}
''
# ${self} is read-only, so bytecode must not be written beside it.
export PYTHONDONTWRITEBYTECODE=1
cd ${self}
python3 -m unittest discover -s build/tests -t . 2>&1 | tee "$out"
'';
# Enforce formatting here: an unformatted commit would otherwise land
# and the next one silently absorb the reformat.
nixfmt-check =
pkgs.runCommand "icedos-nixfmt-check"
{
nativeBuildInputs = [ pkgs.nixfmt ];
}
''
nixfmt --check $(find ${self} -name '*.nix')
echo 'nixfmt: all formatted' > "$out"
'';
}
);
};
}