-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflake.nix
427 lines (390 loc) Β· 13.8 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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
{
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-23.11";
# nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
systems.url = "github:nix-systems/default";
flake-parts.url = "github:hercules-ci/flake-parts";
# flake-utils.url = github:numtide/flake-utils;
poetry2nix = {
url = github:nix-community/poetry2nix;
inputs = {
nixpkgs.follows = "nixpkgs";
# flake-utils.follows = "flake-utils";
};
};
nix2container = {
url = github:nlewo/nix2container;
inputs = {
nixpkgs.follows = "nixpkgs";
};
};
flocken = {
url = "github:mirkolenz/flocken/v2";
inputs.nixpkgs.follows = "nixpkgs";
};
};
nixConfig = {
extra-trusted-public-keys = [
"sciexp.cachix.org-1:HaliIGqJrFN7CDrzYVHqWS4uSISorWAY1bWNmNl8T08="
];
extra-substituters = [
"https://sciexp.cachix.org"
];
};
outputs = inputs @ {
self,
flake-parts,
...
}:
flake-parts.lib.mkFlake {inherit inputs;} {
systems = import inputs.systems;
perSystem = {
self',
system,
...
}: let
pkgs = import inputs.nixpkgs {
inherit system;
overlays = [inputs.poetry2nix.overlays.default];
};
inherit (inputs.nix2container.packages.${system}) nix2container;
# million thanks to @kolloch for the foldImageLayers function!
# https://blog.eigenvalue.net/2023-nix2container-everything-once/
foldImageLayers = let
mergeToLayer = priorLayers: component:
assert builtins.isList priorLayers;
assert builtins.isAttrs component; let
layer = nix2container.buildLayer (component
// {
layers = priorLayers;
});
in
priorLayers ++ [layer];
in
layers: pkgs.lib.foldl mergeToLayer [] layers;
pyPkgsBuildRequirements = {
cloudpickle = ["flit-core"];
feather-format = ["setuptools"];
flytekit = ["setuptools"];
flyteidl = ["setuptools"];
hydra-core = ["setuptools"];
hydra-joblib-launcher = ["setuptools"];
hydra-zen = ["setuptools"];
marshmallow-jsonschema = ["setuptools"];
xdoctest = ["setuptools"];
};
poetry2nixOverrides = pkgs.poetry2nix.overrides.withDefaults (
self: super: let
buildInputsOverrides =
builtins.mapAttrs (
package: buildRequirements:
(builtins.getAttr package super).overridePythonAttrs (old: {
buildInputs =
(old.buildInputs or [])
++ (builtins.map (pkg:
if builtins.isString pkg
then builtins.getAttr pkg super
else pkg)
buildRequirements);
})
)
pyPkgsBuildRequirements;
conditionalOverrides =
if pkgs.stdenv.isDarwin
then {
grpcio = super.grpcio.override {preferWheel = false;};
}
else {};
in
buildInputsOverrides
// {
hydra-core = super.hydra-core.override {preferWheel = true;};
hydra-joblib-launcher = super.hydra-joblib-launcher.override {preferWheel = true;};
mkdocs-material = super.mkdocs-material.override {preferWheel = false;};
scipy = super.scipy.override {preferWheel = true;};
yarl = super.yarl.override {preferWheel = true;};
}
// conditionalOverrides
);
mkPoetryAttrs = {
projectDir = ./.;
overrides = poetry2nixOverrides;
python = pkgs.python310;
# aarch64 cross-compilation on x86_64 may be intolerably slow if
# preferWheels is disabled. If all of the individual contributors to
# this are identified, it may be possible to use the library-specific
# overrides above and disable the global usage of wheels
preferWheels = true;
groups = [
"test"
];
checkGroups = ["test"];
extras = [];
};
poetryEnv = pkgs.poetry2nix.mkPoetryEnv (
mkPoetryAttrs
// {
extraPackages = ps:
with pkgs; [
python310Packages.pip
];
}
);
gitHubOrg = "sciexp";
packageName = "flytezen";
packageSrcPath = "/root/${packageName}/src";
mkPoetryEnvWithSource = packageName: src:
pkgs.poetry2nix.mkPoetryEnv (
mkPoetryAttrs
// {
groups = ["test" "docs"];
extraPackages = ps:
with pkgs; [
python310Packages.pip
];
editablePackageSources = {
${packageName} = src;
};
}
);
sysPackages = with pkgs;
[
bashInteractive
coreutils
cacert
gnutar
gzip
nix
direnv
]
++ lib.optional (lib.elem system pkgs.shadow.meta.platforms) shadow;
mkRootNss = pkgs.runCommand "mkRootNss" {} ''
mkdir -p $out/etc
cat > $out/etc/passwd <<EOF
root:x:0:0:root user:/var/empty:/bin/sh
nobody:x:65534:65534:nobody:/var/empty:/bin/sh
EOF
cat > $out/etc/group <<EOF
root:x:0:root
nobody:x:65534:
nixbld:x:30000:nobody
EOF
echo "hosts: files dns" > $out/etc/nsswitch.conf
mkdir -p $out/tmp
mkdir -p $out/root
'';
rcRoot = pkgs.runCommand "rcRoot" {} ''
mkdir -p $out/root
cat > $out/root/.zshrc <<EOF
eval "\$(direnv hook zsh)"
eval "\$(starship init zsh)"
eval "\$(atuin init zsh)"
EOF
'';
customNixConf = pkgs.runCommand "custom-nix-conf" {} ''
mkdir -p $out/etc/nix
cat > $out/etc/nix/nix.conf <<EOF
build-users-group = nixbld
experimental-features = nix-command flakes repl-flake
bash-prompt-prefix = (nix:\$name)\040
max-jobs = auto
extra-nix-path = nixpkgs=flake:nixpkgs
trusted-users = root
EOF
'';
devPackages = with pkgs; [
atuin
bat
gawk
gh
git
gnugrep
gnumake
lazygit
man-db
man-pages
neovim
poetry
poethepoet
ripgrep
starship
tree
yq-go
zsh
];
# The local path can be used instead of `builtins.fetchGit` applied to
# the repository source url to be used in `packageGitRepoToContainer` to
# place a copy of the local source in the devcontainer if it does not
# exist on a ref+rev:
# packageGitRepo = ./.;
# OR
# packageGitRepo = builtins.fetchGit ./.;
# should also work as an alternative to directly copying the local repo
# path, see https://github.com/NixOS/nix/pull/7706/files; however, the
# explicit ref+rev should likely be preferred outside of development
# experimentation
packageGitRepo = builtins.fetchGit {
name = "${packageName}-source";
url = "https://github.com/${gitHubOrg}/${packageName}.git";
# the ref is not strictly required when specifying a rev but it should
# be included whenever possible or it may be necessary to include
ref = "main";
# allRefs = true;
# ref = "NN-feature";
# the rev can be omitted transiently in development to track the HEAD
# of a ref but doing so requires `--impure` image builds (this may
# already be required for other reasons, e.g. `builtins.getEnv`)
# rev = "b69ef531088f7a244104bc34f919619f15a8aa8d";
};
# The `chmod -R` command to make the root user's home directory writable
# is only necessary to allow overwriting the source in the devcontainer
# without rebuilding the image. Since it violates the intended
# immutability of the image, it can be disabled in release images or
# when image rebuilds are used to update the source during development.
packageGitRepoToContainer = pkgs.runCommand "copy-package-git-repo" {} ''
mkdir -p $out/root
cp -r ${packageGitRepo} $out/root/${packageName}
chmod -R 755 $out/root
'';
pythonPackages = [
(mkPoetryEnvWithSource packageName packageSrcPath)
];
devcontainerLayers = let
layerDefs = [
{
deps = sysPackages;
}
{
deps = devPackages;
}
{
deps = pythonPackages;
}
];
in
foldImageLayers layerDefs;
devcontainerContents = [
# similar to pkgs.fakeNss
mkRootNss
(pkgs.buildEnv {
name = "root";
paths = sysPackages;
pathsToLink = "/bin";
})
customNixConf
rcRoot
packageGitRepoToContainer
];
devcontainerConfig = {
# Use default empty Entrypoint to completely defer to Cmd for flexible override
Entrypoint = [];
# but provide default Cmd to start zsh
Cmd = [
"${pkgs.bashInteractive}/bin/bash"
"-c"
"${pkgs.zsh}/bin/zsh"
];
User = "root";
WorkingDir = "/root";
Env = [
"PATH=${pkgs.lib.makeBinPath (sysPackages ++ devPackages ++ pythonPackages)}:/root/.nix-profile/bin"
"SSL_CERT_FILE=${pkgs.cacert}/etc/ssl/certs/ca-bundle.crt"
"NIX_PAGER=cat"
"USER=root"
"HOME=/root"
"GIT_REPO_NAME=${builtins.getEnv "GIT_REPO_NAME"}"
"GIT_REF=${builtins.getEnv "GIT_REF"}"
"GIT_SHA=${builtins.getEnv "GIT_SHA"}"
"GIT_SHA_SHORT=${builtins.getEnv "GIT_SHA_SHORT"}"
"PYTHONPATH=${packageSrcPath}:${pkgs.lib.strings.makeSearchPathOutput "" "lib/python3.10/site-packages" pythonPackages}"
];
};
in {
formatter = pkgs.alejandra;
devShells = {
default = pkgs.mkShell {
name = packageName;
buildInputs = with pkgs;
[
(mkPoetryEnvWithSource packageName ./src)
]
++ devPackages;
};
};
packages = {
default = pkgs.poetry2nix.mkPoetryApplication (
mkPoetryAttrs
// {
checkPhase = ''
pytest
'';
}
);
releaseEnv = pkgs.buildEnv {
name = "release-env";
paths = with pkgs; [poetry python310];
};
# Very similar devcontainer images can be constructed with either
# nix2container or dockerTools
devcontainerNix2Container = nix2container.buildImage {
name = "${packageName}nixdev";
# generally prefer the default image hash to manual tagging
# tag = "latest";
initializeNixDatabase = true;
# Setting maxLayers <=127
# maxLayers = 123;
# can be used instead of the manual layer specification below
layers = devcontainerLayers;
copyToRoot = devcontainerContents;
config = devcontainerConfig;
};
# Very similar devcontainer images can be constructed with either
# nix2container or dockerTools
devcontainerDockerTools = pkgs.dockerTools.buildLayeredImage {
name = "${packageName}dev";
# with mkDockerManifest, tags may be automatically generated from
# git metadata
tag = "latest";
created = "now";
# maxLayers <=127; defaults to 100
maxLayers = 123;
contents = devcontainerContents;
config = devcontainerConfig;
};
};
legacyPackages.devcontainerManifest = let
includedSystems = let
envVar = builtins.getEnv "NIX_IMAGE_SYSTEMS";
in
if envVar == ""
then ["x86_64-linux" "aarch64-linux"]
else builtins.filter (sys: sys != "") (builtins.split " " envVar);
in
inputs.flocken.legacyPackages.${system}.mkDockerManifest {
github = {
enable = true;
enableRegistry = false;
token = builtins.getEnv "GH_TOKEN";
};
autoTags = {
branch = false;
};
registries = {
"ghcr.io" = {
enable = true;
repo = "${gitHubOrg}/${packageName}dev";
username = builtins.getEnv "GITHUB_ACTOR";
password = builtins.getEnv "GH_TOKEN";
};
};
version = builtins.getEnv "VERSION";
# aarch64-linux may be disabled for more rapid image builds during
# development. Note the usage of `preferWheels` above as well.
# images = with self.packages; [x86_64-linux.devcontainerDockerTools aarch64-linux.devcontainerDockerTools];
# images = with self.packages; [x86_64-linux.devcontainerDockerTools];
images = builtins.map (sys: self.packages.${sys}.devcontainerDockerTools) includedSystems;
tags = [(builtins.getEnv "GIT_SHA_SHORT") (builtins.getEnv "GIT_SHA") (builtins.getEnv "GIT_REF")];
};
};
};
}