From 59aba7607507193587be7c24229dc71066e3ae87 Mon Sep 17 00:00:00 2001 From: Alex Auvolat Date: Tue, 29 Nov 2022 16:19:19 +0100 Subject: [PATCH] Ability to use taskdir as flake; change nixpkgs syntax --- example/example-batch.hcl | 27 +++++++++++++++++++++++++-- example/example-service.hcl | 22 +++++++++++----------- example/flake.nix | 18 ++++++++++++++++++ nix2/driver.go | 4 ++-- nix2/nix.go | 5 +++-- 5 files changed, 59 insertions(+), 17 deletions(-) create mode 100644 example/flake.nix diff --git a/example/example-batch.hcl b/example/example-batch.hcl index a4dc946..15635f9 100644 --- a/example/example-batch.hcl +++ b/example/example-batch.hcl @@ -12,8 +12,11 @@ job "nix2-example-batch" { driver = "nix2" config { + # Packages contains a list of Nix flakes to include in the environement. + # Entries that start with # will be relative to nixpkgs. + # Otherwise, they are flake names that are passed directly to Nix build packages = [ - "hello" # equivalent to "github:nixos/nixpkgs/nixos-22.05#hello" + "#hello" # equivalent to "github:nixos/nixpkgs/nixos-22.05#hello" ] command = "hello" } @@ -31,7 +34,7 @@ job "nix2-example-batch" { config { packages = [ - "curl", "cacert" + "#curl", "#cacert" ] command = "curl" args = [ @@ -42,5 +45,25 @@ job "nix2-example-batch" { SSL_CERT_FILE = "/etc/ssl/certs/ca-bundle.crt" } } + + # This example show how to use a flake defined from a file + task "nix-hello-flake" { + driver = "nix2" + + config { + # Packages contains a list of Nix flakes to include in the environement. + # Entries that start with # will be relative to nixpkgs. + # Otherwise, they are flake names that are passed directly to Nix build + packages = [ + ".#hello" + ] + command = "hello" + } + + template { + data = file("flake.nix") + destination = "flake.nix" + } + } } } diff --git a/example/example-service.hcl b/example/example-service.hcl index 03ba0d3..2f65435 100644 --- a/example/example-service.hcl +++ b/example/example-service.hcl @@ -13,17 +13,17 @@ job "nix2-example-service" { config { packages = [ - "python3", - "bash", - "coreutils", - "curl", - "nix", - "git", - "cacert", - "strace", - "gnugrep", - "findutils", - "mount", + "#python3", + "#bash", + "#coreutils", + "#curl", + "#nix", + "#git", + "#cacert", + "#strace", + "#gnugrep", + "#findutils", + "#mount", ] command = "python3" args = [ "-m", "http.server", "8080" ] diff --git a/example/flake.nix b/example/flake.nix new file mode 100644 index 0000000..3e48ddb --- /dev/null +++ b/example/flake.nix @@ -0,0 +1,18 @@ +{ + description = "A very basic flake"; + + outputs = { self, nixpkgs }: + let + pkgs = import nixpkgs { system = "x86_64-linux"; }; + hello = pkgs.writeScriptBin "hello" '' + #!${pkgs.bash}/bin/bash + echo "Hello from bash script!" + ''; + in { + + packages.x86_64-linux.hello = hello; + + packages.x86_64-linux.default = self.packages.x86_64-linux.hello; + + }; +} diff --git a/nix2/driver.go b/nix2/driver.go index 96bbf42..cba3bee 100644 --- a/nix2/driver.go +++ b/nix2/driver.go @@ -510,8 +510,8 @@ func (d *Driver) StartTask(cfg *drivers.TaskConfig) (*drivers.TaskHandle, *drive } // Use that repo for all packages not specified from a flake already. for i := range driverConfig.Packages { - if !strings.Contains(driverConfig.Packages[i], "#") && !strings.Contains(driverConfig.Packages[i], "/") { - driverConfig.Packages[i] = nixpkgs + "#" + driverConfig.Packages[i] + if strings.HasPrefix(driverConfig.Packages[i], "#") { + driverConfig.Packages[i] = nixpkgs + driverConfig.Packages[i] } } diff --git a/nix2/nix.go b/nix2/nix.go index 5b94065..badf38c 100644 --- a/nix2/nix.go +++ b/nix2/nix.go @@ -25,7 +25,7 @@ func prepareNixPackages(taskDir string, packages []string, nixpkgs string) (hclu mounts := make(hclutils.MapStrStr) profileLink := filepath.Join(taskDir, "current-profile") - profile, err := nixBuildProfile(packages, profileLink) + profile, err := nixBuildProfile(taskDir, packages, profileLink) if err != nil { return nil, fmt.Errorf("Build of the flakes failed: %v", err) } @@ -71,7 +71,7 @@ func prepareNixPackages(taskDir string, packages []string, nixpkgs string) (hclu return mounts, nil } -func nixBuildProfile(flakes []string, link string) (string, error) { +func nixBuildProfile(taskDir string, flakes []string, link string) (string, error) { cmd := exec.Command("nix", append( []string{ "--extra-experimental-features", "nix-command", @@ -84,6 +84,7 @@ func nixBuildProfile(flakes []string, link string) (string, error) { flakes...)...) stderr := &bytes.Buffer{} cmd.Stderr = stderr + cmd.Dir = taskDir if err := cmd.Run(); err != nil { return "", fmt.Errorf("%v failed: %s. Err: %v", cmd.Args, stderr.String(), err)