@@ -1,8 +1,8 @@
1#!/usr/bin/env fish
1#!/usr/bin/env fish
2# Manage a container named after the current directory.
2# Manage a container named after the current directory.
3−# c init [extra create args] build + load the image, then create the container
3+# c init [-c FILE] [extra create args] build + load the image, then create it
4−# (uses ./container.nix for packages if present)
4+# (packages from FILE, default ./container.nix)
5−# c start start it and attach
5+# c start start it and attach
7set -l name (path basename $PWD)
7set -l name (path basename $PWD)
8# The flake lives next to this script (resolve symlinks so it works from $PATH too).
8# 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)))
11switch $argv[1]
11switch $argv[1]
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]
17+ set -l config container.nix
19+ if set -q _flag_config
20+ set config $_flag_config
13 # Build the OCI archive into a temp dir, load it, then clean up.
24 # Build the OCI archive into a temp dir, load it, then clean up.
14 set -l tmp (mktemp -d)
25 set -l tmp (mktemp -d)
15 set -l archive $tmp/image.tar.gz
26 set -l archive $tmp/image.tar.gz
16 set -l rc 0
27 set -l rc 0
18− if test -e container.nix
29+ if test -e $config
19 # Per-project package set: build an image with exactly these packages.
30 # 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)
21 set -l expr "(builtins.getFlake \"$flake\").lib.\${builtins.currentSystem}.copyWith (import (/. + builtins.getEnv \"NIX_CONTAINER_PKGS\"))"
32 set -l expr "(builtins.getFlake \"$flake\").lib.\${builtins.currentSystem}.copyWith (import (/. + builtins.getEnv \"NIX_CONTAINER_PKGS\"))"
22 set -l copyer (nix build --impure --no-link --print-out-paths --expr "$expr")
33 set -l copyer (nix build --impure --no-link --print-out-paths --expr "$expr")
23 set rc $status
34 set rc $status
@@ -25,6 +36,10 @@ switch $argv[1]
25 $copyer/bin/copy-to "oci-archive:$archive:nix-container:latest"
36 $copyer/bin/copy-to "oci-archive:$archive:nix-container:latest"
26 set rc $status
37 set rc $status
39+ else if test $explicit -eq 1
40+ echo "c: config file not found: $config" >&2
29 nix run "$flake#image" -- "oci-archive:$archive:nix-container:latest"
44 nix run "$flake#image" -- "oci-archive:$archive:nix-container:latest"
30 set rc $status
45 set rc $status
@@ -38,10 +53,10 @@ switch $argv[1]
38 rm -rf $tmp
53 rm -rf $tmp
39 test $rc -eq 0; or exit $rc
54 test $rc -eq 0; or exit $rc
41− container create --name $name --ssh -it $argv[2..-1] nix-container:latest
56+ container create --name $name --ssh -it $argv nix-container:latest
42 case start
57 case start
43 container start -ai $name
58 container start -ai $name
45− echo "usage: c init|start" >&2
60+ echo "usage: c init [-c FILE] [create args] | start" >&2