on master help sign in
Make 'c init' build, load, and clean up the image archive
85eaf39iko
README.md+9−4
@@ -55,14 +55,19 @@ container start -ai nixos
55
55
56You land in `fish`. Exit the shell to stop the container.
56You land in `fish`. Exit the shell to stop the container.
57
57
58−> **Shortcut:** the [`c`](./c) script wraps create/start and names the container
58+> **Shortcut:** the [`c`](./c) script bundles the workflow and names the
59−> after the current directory (handy for per-project containers):
59+> container after the current directory (handy for per-project containers). It
60+> resolves the flake from its own location, so you can symlink it onto your
61+> `$PATH` and run it from any project:
60>
62>
61> ```sh
63> ```sh
62−> ./c init # create; append create args to add mounts, e.g.
64+> ./c init # build + load the image, then create the container
63−> ./c init -v $PWD:/work # (mounts can only be set at creation time)
65+> ./c init -v $PWD:/work # add create-time mounts (set only at creation)
64> ./c start # start and attach
66> ./c start # start and attach
65> ```
67> ```
68+>
69+> `c init` builds the archive in a temp dir, loads it, and deletes it — so no
70+> `image.tar.gz` is left lying around.
66
71
67## Re-running after a rebuild
72## Re-running after a rebuild
68
73
c+9−1
@@ -1,12 +1,20 @@
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] create it (e.g. c init -v $PWD:/work)
3+# c init [extra create args] build + load the image, then create the container
4# c start start it and attach
4# c start start it and attach
5
5
6set -l name (path basename $PWD)
6set -l name (path basename $PWD)
7+# The flake lives next to this script (resolve symlinks so it works from $PATH too).
8+set -l flake (path dirname (path resolve (status filename)))
7
9
8switch $argv[1]
10switch $argv[1]
9 case init
11 case init
12+ # Build the OCI archive into a temp dir, load it, then clean up.
13+ set -l tmp (mktemp -d)
14+ nix run $flake -- $tmp/image.tar.gz; and container image load --input $tmp/image.tar.gz
15+ set -l rc $status
16+ rm -rf $tmp
17+ test $rc -eq 0; or exit $rc
10 container create --name $name --ssh -it $argv[2..-1] nix-container:latest
18 container create --name $name --ssh -it $argv[2..-1] nix-container:latest
11 case start
19 case start
12 container start -ai $name
20 container start -ai $name