-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathflake.nix
More file actions
444 lines (422 loc) · 15.8 KB
/
Copy pathflake.nix
File metadata and controls
444 lines (422 loc) · 15.8 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
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
{
description = "A flake for my photography stuff";
inputs = {
nixpkgs.url = "github:nixos/nixpkgs";
git-hooks.url = "github:cachix/git-hooks.nix";
};
outputs =
inputs@{ self, nixpkgs, ... }:
let
system = "x86_64-linux";
pkgs = nixpkgs.legacyPackages.${system};
lib = pkgs.lib;
t = lib.trivial;
hl = pkgs.haskell.lib;
gcpExtraLibraries = with pkgs; [
coreutils # for readlink, dirname, needed by google-cloud-sdk wrappers
openssh # SSH client for gcloud compute ssh
google-cloud-sdk # provides gcloud and gsutil for GCP operations
];
extraLibraries =
with pkgs;
[
self.packages.${system}.focus-stack # main aligning and focus stacking
self.packages.${system}.zerene-stacker # for zerene-stacker integration
hugin # provides align_image_stack, for alternative aligning
glew # for parallel processing
enblend-enfuse # provides enfuse, for alternative focus stacking
imagemagick # for composing colages and more
exiftool # for extracting metadata
ffmpeg-headless # for extracting imgs from video
libraw # for converting raw files like ARW to tiff, provides dcraw_emu
libheif # for converting heif files
udisks # for mounting devices
]
++ gcpExtraLibraries;
project =
devTools:
let
addBuildTools = (t.flip hl.addBuildTools) devTools;
addExtraLibraries = (t.flip hl.addExtraLibraries) extraLibraries;
in
pkgs.haskellPackages.developPackage {
root = lib.cleanSourceWith {
src = ./.;
filter =
path: type:
let
baseName = baseNameOf path;
relativePath = lib.removePrefix (toString ./. + "/") (toString path);
in
!(
# Exclude shell scripts in root
(
type == "regular"
&& lib.hasSuffix ".sh" baseName
&& (builtins.match "[^/]+\\.sh" relativePath) != null
)
# Exclude specific directories
|| lib.hasPrefix "one-time-scripts" relativePath
|| lib.hasPrefix "old.hs" relativePath
|| lib.hasPrefix "PetteriAimonen-focus-stack" relativePath
);
};
name = "myphoto-unwrapped";
returnShellEnv = !(devTools == [ ]);
modifier = (t.flip t.pipe) [
addBuildTools
addExtraLibraries
hl.dontHaddock
hl.enableStaticLibraries
hl.justStaticExecutables
hl.disableLibraryProfiling
hl.disableExecutableProfiling
((t.flip hl.appendBuildFlags) [
"--ghc-options=-threaded"
"--ghc-options=-rtsopts"
"--ghc-options=-with-rtsopts=-N"
"+RTS"
])
];
};
zerene = import ./zerene-stacker/default.nix inputs system;
gcp = import ./gcp/default.nix inputs system;
in
{
packages.${system} = {
focus-stack = pkgs.focus-stack.overrideDerivation (oldAttrs: {
version = "master";
src = ./PetteriAimonen-focus-stack;
});
myphoto-unwrapped = project [ ];
myphoto-stack-from-github = pkgs.writeShellScriptBin "myphoto-gh-stack" ''
exec nix run --refresh "github:maxhbr/myphoto"#myphoto-stack -- "$@"
'';
myphoto-watch-from-github = pkgs.writeShellScriptBin "myphoto-gh-watch" ''
exec nix run --refresh "github:maxhbr/myphoto"#myphoto-watch -- "$@"
'';
myphoto-align-from-github = pkgs.writeShellScriptBin "myphoto-gh-align" ''
exec nix run --refresh "github:maxhbr/myphoto"#myphoto-align -- "$@"
'';
myphoto-toPNG-from-github = pkgs.writeShellScriptBin "myphoto-gh-toPNG" ''
exec nix run --refresh "github:maxhbr/myphoto"#myphoto-toPNG -- "$@"
'';
myphoto-gallery-from-github = pkgs.writeShellScriptBin "myphoto-gh-gallery" ''
exec nix run --refresh "github:maxhbr/myphoto"#myphoto-gallery -- "$@"
'';
myphoto = pkgs.buildEnv {
name = "myphoto";
paths = [
self.packages.${system}.myphoto-stack-from-github
self.packages.${system}.myphoto-watch-from-github
self.packages.${system}.myphoto-align-from-github
self.packages.${system}.myphoto-toPNG-from-github
self.packages.${system}.myphoto-gallery-from-github
self.packages.${system}.zerene-stacker
];
pathsToLink = [
"/bin"
"/share"
];
nativeBuildInputs = [ pkgs.makeWrapper ];
postBuild = ''
mkdir -p $out/bin
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-stack $out/bin/myphoto-stack \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-stack $out/bin/myphoto-stack-inplace \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--inplace"
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-stack $out/bin/myphoto-stack-inplace-0 \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--inplace" \
--add-flags "--export"
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-stack $out/bin/myphoto-stack-replace \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--replace"
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-stack $out/bin/myphoto-stack-replace-0 \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--replace" \
--add-flags "--export"
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-stack $out/bin/myphoto-stack-dirs \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--dirs"
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-align $out/bin/myphoto-align \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-toPNG $out/bin/myphoto-toPNG \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
makeWrapper ${
self.packages.${system}.myphoto-unwrapped
}/bin/myphoto-gallery $out/bin/myphoto-gallery \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-watch $out/bin/myphoto-watch \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-watch $out/bin/myphoto-import \
--set PATH ${pkgs.lib.makeBinPath extraLibraries} \
--add-flags "--only-import"
makeWrapper ${self.packages.${system}.myphoto-unwrapped}/bin/myphoto-gcp $out/bin/myphoto-gcp \
--set PATH ${pkgs.lib.makeBinPath extraLibraries}
mkdir -p $out/share/myphoto/examples
cp ${./examples/options.json} $out/share/myphoto/examples/options.json
cp ${./examples/watch-options.json} $out/share/myphoto/examples/watch-options.json
cat > $out/bin/myphoto-init <<EOF
#!/bin/sh
set -euo pipefail
config_dir="\$HOME/.myphoto"
mkdir -p "\$config_dir"
for f in options.json watch-options.json; do
target="\$config_dir/\$f"
if [ -e "\$target" ]; then
echo "Skipping \$target (already exists)"
else
${pkgs.coreutils}/bin/install -m 644 "$out/share/myphoto/examples/\$f" "\$target"
echo "Created \$target"
fi
done
EOF
chmod +x $out/bin/myphoto-init
'';
};
inherit (zerene) zerene-stacker;
myphoto-docker =
let
XKB_CONFIG_ROOT = "/usr/share/X11/xkb";
XKB_BIN_DIR = "/usr/bin";
XORG_FONT_PATH = "${pkgs.xorg.fontmiscmisc}/share/fonts/X11/misc,${pkgs.dejavu_fonts}/share/fonts/truetype";
XORG_PREFIX = "${pkgs.xorg.xorgserver}";
# mk_screenshots = ''
# make_xwd_screenshots() (
# set +x
# mkdir -p /output/_xwd
# while : ; do
# ${pkgs.xorg.xwd}/bin/xwd -root -display $DISPLAY -silent -out /output/screenshot.xwd
# ${pkgs.coreutils}/bin/sleep 10
# done
# )
# make_xwd_screenshots &
# '';
entrypoint = pkgs.writeShellScriptBin "entrypoint" ''
#!${pkgs.stdenv.shell}
set -euo pipefail
DISPLAY=:99
export DISPLAY
exec &> >(${pkgs.coreutils}/bin/tee -a /output/entrypoint.log)
set -x
${XORG_PREFIX}/bin/Xvfb "$DISPLAY" \
-screen 0 1920x1080x24 \
-nolisten tcp \
-ac \
-xkbdir ${XKB_CONFIG_ROOT} \
-fp ${XORG_FONT_PATH} \
2>/tmp/Xvfb.log &
xvfb_pid=$!
${pkgs.coreutils}/bin/sleep 1
${self.packages.${system}.myphoto}/bin/myphoto-watch --verbose /input /output "$@"
kill "$xvfb_pid" 2>/dev/null || true
'';
in
pkgs.dockerTools.buildImage {
name = "myphoto";
tag = "latest";
copyToRoot = [
pkgs.bash
pkgs.coreutils
pkgs.xkeyboard_config
pkgs.xorg.fontmiscmisc
pkgs.dejavu_fonts
pkgs.xorg.xauth
pkgs.xorg.xkbcomp
pkgs.xorg.xorgserver
pkgs.xorg.xwd
];
extraCommands = ''
mkdir -p input
mkdir -p output
mkdir -p tmp root/.X11-unix
chmod 1777 tmp root/.X11-unix
mkdir -p usr/bin usr/share/X11
ln -s ${pkgs.xkeyboard_config}/share/X11/xkb usr/share/X11/xkb
'';
config = {
Labels = {
"org.opencontainers.image.title" = "myphoto";
};
Env = [
"TMPDIR=/tmp"
"HOME=/root"
"XDG_RUNTIME_DIR=/root"
"LANG=C.UTF-8"
"LC_ALL=C.UTF-8"
"XKB_CONFIG_ROOT=${XKB_CONFIG_ROOT}"
"XORG_FONT_PATH=${XORG_FONT_PATH}"
"XORG_PREFIX=${XORG_PREFIX}"
];
Entrypoint = [
"${entrypoint}/bin/entrypoint"
];
Cmd = [
"--all"
"--once"
"--clean"
];
WorkingDir = "/output";
};
};
myphoto-docker-in-gcp = gcp.myphoto-docker-in-gcp;
default = self.packages.${system}.myphoto;
};
apps.${system} = {
myphoto-stack = {
type = "app";
program = "${self.packages.${system}.myphoto}/bin/myphoto-stack";
};
myphoto-watch = {
type = "app";
program = "${self.packages.${system}.myphoto}/bin/myphoto-watch";
};
myphoto-align = {
type = "app";
program = "${self.packages.${system}.myphoto}/bin/myphoto-align";
};
myphoto-toPNG = {
type = "app";
program = "${self.packages.${system}.myphoto}/bin/myphoto-toPNG";
};
myphoto-gallery = {
type = "app";
program = "${self.packages.${system}.myphoto}/bin/myphoto-gallery";
};
myphoto-gcp = {
type = "app";
program =
let
docker-image = "${self.packages.${system}.myphoto-docker}";
myphoto-gcp-with-docker-image-argument = pkgs.writeShellScriptBin "myphoto-gcp" ''
#!${pkgs.stdenv.shell}
exec ${self.packages.${system}.myphoto}/bin/myphoto-gcp --docker-image "${docker-image}" "$@"
'';
in
"${myphoto-gcp-with-docker-image-argument}/bin/myphoto-gcp";
};
myphoto-docker-in-gcp = {
type = "app";
program = "${self.packages.${system}.myphoto-docker-in-gcp}/bin/myphoto-docker-in-gcp";
};
zerene-stacker = {
type = "app";
program = "${self.packages.${system}.zerene-stacker}/bin/zerene-stacker";
};
zerene-stacker-batch = {
type = "app";
program = "${self.packages.${system}.zerene-stacker}/bin/zerene-stacker-batch";
};
zerene-stacker-batch-headless = {
type = "app";
program = "${self.packages.${system}.zerene-stacker}/bin/zerene-stacker-batch-headless";
};
};
formatter.${system} =
let
pkgs = nixpkgs.legacyPackages.${system};
config = self.checks.${system}.pre-commit-check.config;
inherit (config) package configFile;
script = ''
${pkgs.lib.getExe package} run --all-files --config ${configFile}
'';
in
pkgs.writeShellScriptBin "pre-commit-run" script;
checks.${system} = {
pre-commit-check = inputs.git-hooks.lib.${system}.run {
src = ./.;
hooks = {
nixfmt-rfc-style.enable = true;
shfmt.enable = false;
shfmt.settings.simplify = true;
# shellcheck.enable = true;
ormolu.enable = true;
};
};
shell-fmt-check =
let
pkgs = inputs.nixpkgs.legacyPackages."${system}";
files = pkgs.lib.concatStringsSep " " [
"myphoto-stack.sh"
"myphoto-watch.sh"
"myphoto-gallery.sh"
"myphoto-align.sh"
"myphoto-toPNG.sh"
"myphoto-gcp.sh"
"gcp/run-myphoto-in-gcp.sh"
"gcp/myphoto-remote-provision.sh"
"gcp/myphoto-remote-execute.sh"
"app-gcp/remote/provision.sh"
"app-gcp/remote/execute.sh"
];
in
pkgs.stdenv.mkDerivation {
name = "shell-fmt-check";
src = ./.;
doCheck = true;
nativeBuildInputs = with pkgs; [
shfmt
];
checkPhase = ''
shfmt -d -s -i 4 -ci ${files}
'';
installPhase = ''
mkdir "$out"
'';
};
};
devShell.${system} =
let
inherit (self.checks.${system}.pre-commit-check) shellHook enabledPackages;
in
project (
with pkgs.haskellPackages;
[
cabal-fmt
cabal-install
haskell-language-server
ormolu
hlint
ghcid
]
++ (with pkgs; [
shfmt
])
++ enabledPackages
);
homeManagerModules.myphoto = (
{
config,
lib,
pkgs,
...
}:
let
inherit (pkgs.stdenv.hostPlatform) system;
in
{
config = {
home.packages = (
with self.packages.${system};
[
focus-stack
myphoto
]
);
};
}
);
};
}