cmd/nix-tool: override context for chroot store

This commit is contained in:
Ophestra 2025-07-20 02:15:18 +09:00
parent 0956524d0c
commit d6d327cb91
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
2 changed files with 25 additions and 0 deletions

17
cmd/nix-tool/chroot.go Normal file
View File

@ -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...)...)
}

View File

@ -10,6 +10,7 @@ import (
"os" "os"
"os/signal" "os/signal"
"slices" "slices"
"strings"
"syscall" "syscall"
"gensokyo.uk/nix" "gensokyo.uk/nix"
@ -27,6 +28,7 @@ func main() {
defer stop() defer stop()
var ( var (
flagStore string
flagNixOS bool flagNixOS bool
flagVerbose bool flagVerbose bool
flagJSON bool flagJSON bool
@ -40,8 +42,14 @@ func main() {
} }
ctx = nix.New(nixCtx, nil, os.Stdout, stderr) ctx = nix.New(nixCtx, nil, os.Stdout, stderr)
flagStore = strings.TrimSpace(flagStore)
if flagStore != string(os.PathSeparator) {
ctx = &chrootStoreContext{flagStore, ctx}
}
return nil 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(&flagNixOS, "nixos", command.BoolFlag(false), "Interpret input as NixOS flake installable").
Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr"). Flag(&flagVerbose, "v", command.BoolFlag(false), "Connect nix stderr").
Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable") Flag(&flagJSON, "json", command.BoolFlag(false), "Serialise output in JSON when applicable")