diff --git a/cmd/nix-tool/chroot.go b/cmd/nix-tool/chroot.go new file mode 100644 index 0000000..79d8f60 --- /dev/null +++ b/cmd/nix-tool/chroot.go @@ -0,0 +1,17 @@ +package main + +import ( + "context" + "os/exec" + + "gensokyo.uk/nix" +) + +type chrootStoreContext struct { + store string + nix.Context +} + +func (c *chrootStoreContext) Nix(ctx context.Context, arg ...string) *exec.Cmd { + return c.Context.Nix(ctx, append([]string{"--store", c.store}, arg...)...) +} diff --git a/cmd/nix-tool/main.go b/cmd/nix-tool/main.go index 912cdde..c313402 100644 --- a/cmd/nix-tool/main.go +++ b/cmd/nix-tool/main.go @@ -10,6 +10,7 @@ import ( "os" "os/signal" "slices" + "strings" "syscall" "gensokyo.uk/nix" @@ -27,6 +28,7 @@ func main() { defer stop() var ( + flagStore string flagNixOS bool flagVerbose bool flagJSON bool @@ -40,8 +42,14 @@ func main() { } ctx = nix.New(nixCtx, nil, os.Stdout, stderr) + flagStore = strings.TrimSpace(flagStore) + if flagStore != string(os.PathSeparator) { + ctx = &chrootStoreContext{flagStore, ctx} + } + return nil }). + Flag(&flagStore, "store", command.StringFlag("/"), "Path to the nix root"). Flag(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable"). Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr"). Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable")