Skip to content

Commit

Permalink
Add NewExternalNixBuilder to enable config-level changing of NixBuilder
Browse files Browse the repository at this point in the history
  • Loading branch information
elpdt852 committed Feb 14, 2024
1 parent ee73e9b commit 58c812e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 11 deletions.
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,12 @@ func serve(ctx context.Context, cfg *config.Config) error {
runtime.RegisterImageServiceServer(rpc, imageService)
}

sn, err := nix.NewSnapshotter(cfg.Root)
var snapshotterOpts []nix.SnapshotterOpt
if cfg.ExternalBuilder != "" {
snapshotterOpts = append(snapshotterOpts, nix.WithNixBuilder(nix.NewExternalBuilder(cfg.ExternalBuilder)))
}

sn, err := nix.NewSnapshotter(cfg.Root, snapshotterOpts...)
if err != nil {
return err
}
Expand Down
43 changes: 37 additions & 6 deletions modules/nixos/tests/snapshotter.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ lib, ... }:
{ pkgs, lib, ... }:
let
registryHost = "127.0.0.1";

Expand Down Expand Up @@ -50,8 +50,11 @@ let
base
../nix-snapshotter.nix
];
services.nix-snapshotter.setContainerdSnapshotter = true;
services.nix-snapshotter.enable = true;

services.nix-snapshotter = {
enable = true;
setContainerdSnapshotter = true;
};
};

rootless = {
Expand All @@ -60,8 +63,11 @@ let
../nix-snapshotter.nix
../nix-snapshotter-rootless.nix
];
services.nix-snapshotter.rootless.setContainerdSnapshotter = true;
services.nix-snapshotter.rootless.enable = true;

services.nix-snapshotter.rootless = {
enable = true;
setContainerdSnapshotter = true;
};

users.users.alice = {
uid = 1000;
Expand All @@ -71,8 +77,30 @@ let

both = { imports = [ rootful rootless ]; };

external = {
imports = [
base
../nix-snapshotter.nix
];

services.nix-snapshotter = {
enable = true;
setContainerdSnapshotter = true;
settings.external_builder = pkgs.writeScript "external-builder.sh" ''
${pkgs.nix}/bin/nix build --out-link $1 $2
'';
};
};

in {
nodes = { inherit rootful rootless both; };
nodes = {
inherit
rootful
rootless
both
external
;
};

testScript = { nodes, ... }:
let
Expand Down Expand Up @@ -150,5 +178,8 @@ in {
setup(both)
test_rootful(both, "both")
test_rootless(both, "both")
setup(external)
test_rootful(external)
'';
}
7 changes: 4 additions & 3 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ var (

// Config provides nix-snapshotter configuration data.
type Config struct {
Address string `toml:"address"`
Root string `toml:"root"`
ImageService ImageServiceConfig `toml:"image_service"`
Address string `toml:"address"`
Root string `toml:"root"`
ExternalBuilder string `toml:"external_builder"`
ImageService ImageServiceConfig `toml:"image_service"`
}

type ImageServiceConfig struct {
Expand Down
14 changes: 14 additions & 0 deletions pkg/nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,17 @@ func defaultNixBuilder(ctx context.Context, outLink, nixStorePath string) error
}
return err
}

// NewExternalBuilder returns a NixBuilder from an external executable with
// two arguments: an out-link path, and a Nix store path.
func NewExternalBuilder(name string) NixBuilder {
return func(ctx context.Context, outLink, nixStorePath string) error {
out, err := exec.Command(name, outLink, nixStorePath).CombinedOutput()
if err != nil {
log.G(ctx).
WithField("nixStorePath", nixStorePath).
Errorf("Failed to run external nix builder: %s\n%s", err, string(out))
}
return err
}
}
8 changes: 7 additions & 1 deletion pkg/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ func init() {
}

ic.Meta.Exports["root"] = root
return nix.NewSnapshotter(root)

var snapshotterOpts []nix.SnapshotterOpt
if cfg.ExternalBuilder != "" {
snapshotterOpts = append(snapshotterOpts, nix.WithNixBuilder(nix.NewExternalBuilder(cfg.ExternalBuilder)))
}

return nix.NewSnapshotter(root, snapshotterOpts...)
},
})
}

0 comments on commit 58c812e

Please sign in to comment.