@@ -1,8 +1,8 @@
11#!/usr/bin/env fish
22# Manage a container named after the current directory.
3−# c init [extra create args] build + load the image, then create the container
4−# (uses ./container.nix for packages if present)
5−# c start start it and attach
3+# c init [-c FILE] [extra create args] build + load the image, then create it
4+# (packages from FILE, default ./container.nix)
5+# c start start it and attach
66
77set -l name (path basename $PWD)
88# The flake lives next to this script (resolve symlinks so it works from $PATH too).
@@ -10,14 +10,25 @@ set -l flake (path dirname (path resolve (status filename)))
1010
1111switch $argv[1]
1212 case init
13+ # -c/--config overrides the default ./container.nix; leftover args (e.g.
14+ # mounts) are forwarded to `container create`.
15+ argparse --ignore-unknown 'c/config=' -- $argv[2..-1]
16+ or exit 1
17+ set -l config container.nix
18+ set -l explicit 0
19+ if set -q _flag_config
20+ set config $_flag_config
21+ set explicit 1
22+ end
23+
1324 # Build the OCI archive into a temp dir, load it, then clean up.
1425 set -l tmp (mktemp -d)
1526 set -l archive $tmp/image.tar.gz
1627 set -l rc 0
1728
18− if test -e container.nix
29+ if test -e $config
1930 # Per-project package set: build an image with exactly these packages.
20− set -lx NIX_CONTAINER_PKGS (path resolve container.nix)
31+ set -lx NIX_CONTAINER_PKGS (path resolve $config)
2132 set -l expr "(builtins.getFlake \"$flake\").lib.\${builtins.currentSystem}.copyWith (import (/. + builtins.getEnv \"NIX_CONTAINER_PKGS\"))"
2233 set -l copyer (nix build --impure --no-link --print-out-paths --expr "$expr")
2334 set rc $status
@@ -25,6 +36,10 @@ switch $argv[1]
2536 $copyer/bin/copy-to "oci-archive:$archive:nix-container:latest"
2637 set rc $status
2738 end
39+ else if test $explicit -eq 1
40+ echo "c: config file not found: $config" >&2
41+ rm -rf $tmp
42+ exit 1
2843 else
2944 nix run "$flake#image" -- "oci-archive:$archive:nix-container:latest"
3045 set rc $status
@@ -38,10 +53,10 @@ switch $argv[1]
3853 rm -rf $tmp
3954 test $rc -eq 0; or exit $rc
4055
41− container create --name $name --ssh -it $argv[2..-1] nix-container:latest
56+ container create --name $name --ssh -it $argv nix-container:latest
4257 case start
4358 container start -ai $name
4459 case '*'
45− echo "usage: c init|start" >&2
60+ echo "usage: c init [-c FILE] [create args] | start" >&2
4661 exit 1
4762end