treewide: migrate to gensokyo.uk/nix

This commit is contained in:
Ophestra 2025-07-19 02:33:24 +09:00
parent 92ac3275c1
commit fdc969afb6
Signed by: cat
SSH Key Fingerprint: SHA256:gQ67O0enBZ7UdZypgtspB2FDM1g3GVw8nX0XSdcFw8Q
19 changed files with 87 additions and 88 deletions

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"context"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"errors"
@ -9,7 +9,7 @@ import (
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
"hakurei.app/command"
)
@ -23,14 +23,14 @@ func init() {
flagBuildPBL bool
flagBuildVerbose bool
)
c.NewCommand(nixbuild.CommandBuild, "emit samples for various `nix build` cases", func(args []string) error {
c.NewCommand(nix.CommandBuild, "emit samples for various `nix build` cases", func(args []string) error {
switch {
default:
return os.ErrInvalid
case len(args) == 7 && slices.Equal(args[1:], []string{
nixbuild.FlagOption, nixbuild.OptionEvalCache, nixbuild.ValueFalse,
nixbuild.FlagDryRun, nixbuild.FlagPrintBuildLogs, nixbuild.FlagDebug,
nix.FlagOption, nix.OptionEvalCache, nix.ValueFalse,
nix.FlagDryRun, nix.FlagPrintBuildLogs, nix.FlagDebug,
}): // InstantiatedEvaluator
return stubInstantiatedEvaluator(args)
@ -53,12 +53,12 @@ func init() {
}
}
}).
Flag(&flagBuildKeepGoing, trimFlagName(nixbuild.FlagKeepGoing), command.BoolFlag(false), nixbuild.FlagKeepGoing).
Flag(&flagBuildNoLink, trimFlagName(nixbuild.FlagNoLink), command.BoolFlag(false), nixbuild.FlagNoLink).
Flag(&flagBuildStdin, trimFlagName(nixbuild.FlagStdin), command.BoolFlag(false), nixbuild.FlagStdin).
Flag(&flagBuildQuiet, trimFlagName(nixbuild.FlagQuiet), command.BoolFlag(false), nixbuild.FlagQuiet).
Flag(&flagBuildPBL, trimFlagName(nixbuild.FlagPrintBuildLogs), command.BoolFlag(false), nixbuild.FlagPrintBuildLogs).
Flag(&flagBuildVerbose, trimFlagName(nixbuild.FlagVerbose), command.BoolFlag(false), nixbuild.FlagVerbose)
Flag(&flagBuildKeepGoing, trimFlagName(nix.FlagKeepGoing), command.BoolFlag(false), nix.FlagKeepGoing).
Flag(&flagBuildNoLink, trimFlagName(nix.FlagNoLink), command.BoolFlag(false), nix.FlagNoLink).
Flag(&flagBuildStdin, trimFlagName(nix.FlagStdin), command.BoolFlag(false), nix.FlagStdin).
Flag(&flagBuildQuiet, trimFlagName(nix.FlagQuiet), command.BoolFlag(false), nix.FlagQuiet).
Flag(&flagBuildPBL, trimFlagName(nix.FlagPrintBuildLogs), command.BoolFlag(false), nix.FlagPrintBuildLogs).
Flag(&flagBuildVerbose, trimFlagName(nix.FlagVerbose), command.BoolFlag(false), nix.FlagVerbose)
})
}
@ -66,7 +66,7 @@ func TestBuild(t *testing.T) {
stubNixCommand(t)
check := func(stdout, stderr io.Writer, v ...string) {
t.Run(strings.Join(v, " "), func(t *testing.T) {
if err := nixbuild.Build(
if err := nix.Build(
newStubContext(t.Context(), nil, stdout, stderr),
slices.Values(v),
); err != nil {
@ -84,8 +84,8 @@ func TestBuild(t *testing.T) {
func TestBuildBadCommand(t *testing.T) {
wantErr := os.ErrNotExist
breakNixCommand(t)
if err := nixbuild.Build(
nixbuild.New(t.Context(), nil, nil, nil),
if err := nix.Build(
nix.New(t.Context(), nil, nil, nil),
nil,
); !errors.Is(err, wantErr) {
t.Errorf("Build: error = %v, want %v", err, wantErr)

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"context"

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"context"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"encoding/json"
@ -9,13 +9,13 @@ import (
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
"hakurei.app/command"
)
func init() {
stubCommandInit = append(stubCommandInit, func(c command.Command) {
c.NewCommand(nixbuild.CommandDerivation, "emit samples for various `nix derivation` cases", func(args []string) error {
c.NewCommand(nix.CommandDerivation, "emit samples for various `nix derivation` cases", func(args []string) error {
if len(args) == 0 {
return syscall.ENOSYS
}
@ -24,7 +24,7 @@ func init() {
default:
return syscall.EINVAL
case nixbuild.CommandDerivationShow:
case nix.CommandDerivationShow:
if len(args) != 2 {
return syscall.ENOSYS
}
@ -33,9 +33,9 @@ func init() {
default:
return syscall.EINVAL
case nixbuild.FlagStdin:
case nix.FlagStdin:
var testName string
if want, err := nixbuild.ReadStdin(os.Stdin); err != nil {
if want, err := nix.ReadStdin(os.Stdin); err != nil {
return err
} else {
for n, w := range instWant {
@ -72,7 +72,7 @@ func TestDerivationShow(t *testing.T) {
{"pluiedev pappardelle"},
}
for _, tc := range testCases {
got, err := nixbuild.DerivationShow(
got, err := nix.DerivationShow(
newStubContext(t.Context(), nil, os.Stdout, os.Stderr),
slices.Values(instWant[tc.name]),
)
@ -80,7 +80,7 @@ func TestDerivationShow(t *testing.T) {
t.Fatalf("DerivationShow: error = %v", err)
}
var want nixbuild.DerivationMap
var want nix.DerivationMap
if err = json.Unmarshal(drvShow[tc.name], &want); err != nil {
t.Fatalf("cannot unmarshal: %v", err)
}
@ -93,7 +93,7 @@ func TestDerivationShow(t *testing.T) {
func TestDerivationShowAlreadySet(t *testing.T) {
stubNixCommand(t)
if _, err := nixbuild.DerivationShow(
if _, err := nix.DerivationShow(
newStubContextCommand(func(cmd *exec.Cmd) { cmd.Stdout = os.Stdout }, t.Context(), nil, os.Stdout, os.Stderr),
nil,
); err == nil {
@ -102,7 +102,7 @@ func TestDerivationShowAlreadySet(t *testing.T) {
}
func BenchmarkShowUnmarshal(b *testing.B) {
var v nixbuild.DerivationMap
var v nix.DerivationMap
data := drvShow["pluiedev pappardelle"]
for b.Loop() {
if err := json.Unmarshal(data, &v); err != nil {

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"context"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"errors"
@ -8,13 +8,12 @@ import (
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
)
func TestNixWriteStdin(t *testing.T) {
ctx := nixbuild.New(t.Context(), nil, os.Stdout, os.Stderr)
t.Run("already set", func(t *testing.T) {
ctx := nix.New(t.Context(), nil, os.Stdout, os.Stderr)
cmd := exec.CommandContext(t.Context(), "/proc/nonexistent")
cmd.Stdin = os.Stdin
if _, err := ctx.WriteStdin(cmd, nil, nil); err == nil {

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"bufio"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"errors"
@ -7,7 +7,7 @@ import (
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
)
func TestStdin(t *testing.T) {
@ -25,7 +25,7 @@ func TestStdin(t *testing.T) {
t.Run("write", func(t *testing.T) {
w := new(strings.Builder)
if _, err := nixbuild.WriteStdin(w, slices.Values(tc.col)); err != nil {
if _, err := nix.WriteStdin(w, slices.Values(tc.col)); err != nil {
t.Fatalf("cannot write: %v", err)
}
if w.String() != tc.raw {
@ -35,7 +35,7 @@ func TestStdin(t *testing.T) {
t.Run("read", func(t *testing.T) {
r := strings.NewReader(tc.raw)
got, err := nixbuild.ReadStdin(r)
got, err := nix.ReadStdin(r)
if err != nil {
t.Fatalf("cannot read: %v", err)
}
@ -47,7 +47,7 @@ func TestStdin(t *testing.T) {
}
t.Run("write error", func(t *testing.T) {
n, err := nixbuild.WriteStdin(errorWriter{}, slices.Values([]string{"/nix/store"}))
n, err := nix.WriteStdin(errorWriter{}, slices.Values([]string{"/nix/store"}))
if n != 0 {
panic("unreachable")
}
@ -65,7 +65,7 @@ func TestInstallable(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
got := nixbuild.NixOSInstallable(tc.flake, tc.host)
got := nix.NixOSInstallable(tc.flake, tc.host)
if got != tc.want {
t.Errorf("Installable(%q, %q): %q, want %q",
tc.flake, tc.host, got, tc.want)

2
go.mod
View File

@ -1,4 +1,4 @@
module git.gensokyo.uk/yonah/nixbuild
module gensokyo.uk/nix
go 1.24.4

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"bufio"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"errors"
@ -9,7 +9,7 @@ import (
"strings"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
)
func TestInstantiated(t *testing.T) {
@ -20,11 +20,11 @@ func TestInstantiated(t *testing.T) {
want []string
wantErr error
}{
{"bad fields", nil, &nixbuild.MalformedInstantiatedError{Type: nixbuild.InstantiatedBadFields}},
{"unexpected quotes left", nil, &nixbuild.MalformedInstantiatedError{Type: nixbuild.InstantiatedUnexpectedQuotes}},
{"unexpected quotes right", nil, &nixbuild.MalformedInstantiatedError{Type: nixbuild.InstantiatedUnexpectedQuotes}},
{"unexpected quotes short", nil, &nixbuild.MalformedInstantiatedError{Type: nixbuild.InstantiatedUnexpectedQuotes}},
{"not absolute", nil, &nixbuild.MalformedInstantiatedError{Type: nixbuild.InstantiatedNotAbsolute}},
{"bad fields", nil, &nix.MalformedInstantiatedError{Type: nix.InstantiatedBadFields}},
{"unexpected quotes left", nil, &nix.MalformedInstantiatedError{Type: nix.InstantiatedUnexpectedQuotes}},
{"unexpected quotes right", nil, &nix.MalformedInstantiatedError{Type: nix.InstantiatedUnexpectedQuotes}},
{"unexpected quotes short", nil, &nix.MalformedInstantiatedError{Type: nix.InstantiatedUnexpectedQuotes}},
{"not absolute", nil, &nix.MalformedInstantiatedError{Type: nix.InstantiatedNotAbsolute}},
{"good segment", []string{
"/nix/store/3zilrlmq7r6rpzfd94mwss32b62yinj5-bootstrap-stage0-stdenv-linux.drv",
@ -49,7 +49,7 @@ func TestInstantiated(t *testing.T) {
t.Run("decoder", func(t *testing.T) {
out := strings.NewReader(sample)
decoder := nixbuild.NewInstantiatedDecoder(out, stderr)
decoder := nix.NewInstantiatedDecoder(out, stderr)
got, err := decoder.Decode()
if !errors.Is(err, tc.wantErr) {
t.Fatalf("Decode: error = %v, want %v", err, tc.wantErr)
@ -71,7 +71,7 @@ func TestInstantiated(t *testing.T) {
t.Run("evaluator", func(t *testing.T) {
ctx := newStubContext(t.Context(), nil, os.Stdout, stderr)
got, err := nixbuild.EvalInstantiated(ctx, tc.name)
got, err := nix.EvalInstantiated(ctx, tc.name)
if !errors.Is(err, tc.wantErr) {
t.Fatalf("EvalInstantiated: error = %v, want %v", err, tc.wantErr)
}
@ -99,7 +99,7 @@ func TestInstantiatedDecoderStopEarly(t *testing.T) {
"/nix/store/nbsdqpfzh1jlpmh95s69b3iivfcvv3lh-config.sub-948ae97.drv",
}
decoder := nixbuild.NewInstantiatedDecoder(strings.NewReader(segmentPrefix+segmentBody+segmentSuffix), os.Stderr)
decoder := nix.NewInstantiatedDecoder(strings.NewReader(segmentPrefix+segmentBody+segmentSuffix), os.Stderr)
counter := 3
got := make([]string, 0, counter)
for drv := range decoder.Instantiated() {
@ -119,8 +119,8 @@ func TestInstantiatedEvaluatorBadCommand(t *testing.T) {
wantErr := os.ErrNotExist
breakNixCommand(t)
if _, err := nixbuild.EvalInstantiated(
nixbuild.New(t.Context(), nil, os.Stdout, os.Stderr),
if _, err := nix.EvalInstantiated(
nix.New(t.Context(), nil, os.Stdout, os.Stderr),
"",
); !errors.Is(err, wantErr) {
t.Errorf("EvalInstantiated: error = %v, want %v", err, wantErr)
@ -129,7 +129,7 @@ func TestInstantiatedEvaluatorBadCommand(t *testing.T) {
func TestInstantiatedEvaluatorAlreadySet(t *testing.T) {
stubNixCommand(t)
if _, err := nixbuild.EvalInstantiated(
if _, err := nix.EvalInstantiated(
newStubContextCommand(func(cmd *exec.Cmd) { cmd.Stderr = os.Stderr }, t.Context(), nil, os.Stdout, os.Stderr),
"",
); err == nil {
@ -140,13 +140,13 @@ func TestInstantiatedEvaluatorAlreadySet(t *testing.T) {
func TestMalformedInstantiatedError(t *testing.T) {
t.Run("bad type", func(t *testing.T) {
badErr := errors.New("")
if errors.Is(new(nixbuild.MalformedInstantiatedError), badErr) {
if errors.Is(new(nix.MalformedInstantiatedError), badErr) {
t.Error("unexpected MalformedInstantiatedError equivalence")
}
})
t.Run("nil", func(t *testing.T) {
if errors.Is(new(nixbuild.MalformedInstantiatedError), (*nixbuild.MalformedInstantiatedError)(nil)) {
if errors.Is(new(nix.MalformedInstantiatedError), (*nix.MalformedInstantiatedError)(nil)) {
t.Error("unexpected MalformedInstantiatedError equivalence")
}
})
@ -158,12 +158,12 @@ func TestMalformedInstantiatedError(t *testing.T) {
t.Errorf("Error: panic = %q, want %q", r, wantPanic)
}
}()
_ = (&nixbuild.MalformedInstantiatedError{Type: -1}).Error()
_ = (&nix.MalformedInstantiatedError{Type: -1}).Error()
})
}
func benchmarkInstantiatedDecoder(b *testing.B, out string) {
decoder := nixbuild.NewInstantiatedDecoder(strings.NewReader(out), nil)
decoder := nix.NewInstantiatedDecoder(strings.NewReader(out), nil)
for b.Loop() {
retry:
if !decoder.Scan() {
@ -171,7 +171,7 @@ func benchmarkInstantiatedDecoder(b *testing.B, out string) {
if err := decoder.Err(); err != nil {
b.Fatalf("Decode: error = %v", err)
}
decoder = nixbuild.NewInstantiatedDecoder(strings.NewReader(out), nil)
decoder = nix.NewInstantiatedDecoder(strings.NewReader(out), nil)
b.StartTimer()
goto retry
}
@ -189,7 +189,7 @@ func BenchmarkInstantiatedDecoder(b *testing.B) {
func benchmarkInstantiated(b *testing.B, out string, want []string) {
for b.Loop() {
v, _ := nixbuild.NewInstantiatedDecoder(strings.NewReader(out), nil).Decode()
v, _ := nix.NewInstantiatedDecoder(strings.NewReader(out), nil).Decode()
b.StopTimer()
if !slices.Equal(v, want) {
b.Fatalf("Decode: %#v, want %#v", v, want)

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import _ "embed"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import _ "embed"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
_ "embed"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import "strings"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"context"
@ -12,7 +12,7 @@ import (
"syscall"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
"hakurei.app/command"
)
@ -29,9 +29,9 @@ var (
// stubNixCommand causes all nix command invocations to invoke the stub for the current test.
func stubNixCommand(t *testing.T) {
n := nixbuild.Nix
nixbuild.Nix = os.Args[0]
t.Cleanup(func() { nixbuild.Nix = n })
n := nix.Nix
nix.Nix = os.Args[0]
t.Cleanup(func() { nix.Nix = n })
cur, ok := os.LookupEnv(runAsNixStub)
if err := os.Setenv(runAsNixStub, "1"); err != nil {
@ -51,14 +51,14 @@ func stubNixCommand(t *testing.T) {
}
// newStubContext creates a context for use with the nix command stub.
func newStubContext(ctx context.Context, extraArgs []string, stdout, stderr io.Writer) nixbuild.Context {
return nixbuild.New(ctx, append(stubExtraArgs, extraArgs...), stdout, stderr)
func newStubContext(ctx context.Context, extraArgs []string, stdout, stderr io.Writer) nix.Context {
return nix.New(ctx, append(stubExtraArgs, extraArgs...), stdout, stderr)
}
type stubContextCommand struct {
f func(*exec.Cmd)
nixbuild.Context
nix.Context
}
func (s *stubContextCommand) Nix(ctx context.Context, arg ...string) *exec.Cmd {
@ -70,15 +70,15 @@ func (s *stubContextCommand) Nix(ctx context.Context, arg ...string) *exec.Cmd {
}
// newStubContext creates a context for use with the nix command stub with a function injected into [nixbuild.Context.Nix].
func newStubContextCommand(f func(*exec.Cmd), ctx context.Context, extraArgs []string, stdout, stderr io.Writer) nixbuild.Context {
func newStubContextCommand(f func(*exec.Cmd), ctx context.Context, extraArgs []string, stdout, stderr io.Writer) nix.Context {
return &stubContextCommand{f, newStubContext(ctx, extraArgs, stdout, stderr)}
}
// breakNixCommand makes all nix invocations fail for the current test.
func breakNixCommand(t *testing.T) {
n := nixbuild.Nix
nixbuild.Nix = "/proc/nonexistent"
t.Cleanup(func() { nixbuild.Nix = n })
n := nix.Nix
nix.Nix = "/proc/nonexistent"
t.Cleanup(func() { nix.Nix = n })
}
type stubCommandInitFunc func(c command.Command)
@ -95,15 +95,15 @@ func TestNixStub(t *testing.T) {
flagExtraExperimentalFeatures string
)
c := command.New(os.Stderr, t.Logf, "nix", func(args []string) error {
if flagExtraExperimentalFeatures != nixbuild.ExperimentalFeaturesFlakes {
if flagExtraExperimentalFeatures != nix.ExperimentalFeaturesFlakes {
t.Fatalf("%s: %q, want %q",
nixbuild.ExtraExperimentalFeatures, flagExtraExperimentalFeatures, nixbuild.ExperimentalFeaturesFlakes)
nix.ExtraExperimentalFeatures, flagExtraExperimentalFeatures, nix.ExperimentalFeaturesFlakes)
return syscall.ENOTRECOVERABLE
}
return nil
}).
Flag(&flagExtraExperimentalFeatures, trimFlagName(nixbuild.ExtraExperimentalFeatures), command.StringFlag(""),
fmt.Sprintf("expects exactly %q", nixbuild.ExperimentalFeaturesFlakes))
Flag(&flagExtraExperimentalFeatures, trimFlagName(nix.ExtraExperimentalFeatures), command.StringFlag(""),
fmt.Sprintf("expects exactly %q", nix.ExperimentalFeaturesFlakes))
c.Command("true", command.UsageInternal, func([]string) error { return nil })
@ -120,7 +120,7 @@ func TestNixStub(t *testing.T) {
// checkStdin checks whether entries read from r is equivalent to want.
func checkStdin(r io.Reader, want ...string) error {
if got, err := nixbuild.ReadStdin(r); err != nil {
if got, err := nix.ReadStdin(r); err != nil {
return err
} else if !slices.Equal(got, want) {
return errors.New(fmt.Sprintf("got build %#v, want %#v", got, want))

View File

@ -1,4 +1,4 @@
package nixbuild
package nix
import (
"path"

View File

@ -1,4 +1,4 @@
package nixbuild_test
package nix_test
import (
"encoding/json"
@ -6,7 +6,7 @@ import (
"strings"
"testing"
"git.gensokyo.uk/yonah/nixbuild"
"gensokyo.uk/nix"
)
func TestCollectFromDerivations(t *testing.T) {
@ -19,11 +19,11 @@ func TestCollectFromDerivations(t *testing.T) {
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
var derivations nixbuild.DerivationMap
var derivations nix.DerivationMap
if err := json.Unmarshal(drvShow[tc.name], &derivations); err != nil {
t.Fatalf("cannot unmarshal test data: %v", err)
}
got := nixbuild.CollectFromDerivations(derivations)
got := nix.CollectFromDerivations(derivations)
want := collectWant[tc.name]
if !slices.Equal(got, want) {
t.Errorf("CollectFromDerivations:\n%s, want\n%s",
@ -35,9 +35,9 @@ func TestCollectFromDerivations(t *testing.T) {
t.Run("edge cases", func(t *testing.T) {
// this exclusively tests edge cases for nil values and buffer growing, so the data doesn't have to make sense
want := []string{"", "big"}
got := nixbuild.CollectFromDerivations(nixbuild.DerivationMap{
got := nix.CollectFromDerivations(nix.DerivationMap{
"nil": nil,
"big": &nixbuild.Derivation{InputSources: make([]string, 1<<18)},
"big": &nix.Derivation{InputSources: make([]string, 1<<18)},
})
if !slices.Equal(got, want) {
t.Errorf("CollectFromDerivations: %#v, want %#v", got, want)