Ability to use taskdir as flake; change nixpkgs syntax

This commit is contained in:
Alex 2022-11-29 16:19:19 +01:00
parent 8b424e1950
commit 59aba76075
Signed by: lx
GPG key ID: 0E496D15096376BE
5 changed files with 59 additions and 17 deletions

View file

@ -12,8 +12,11 @@ job "nix2-example-batch" {
driver = "nix2" driver = "nix2"
config { 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 = [ packages = [
"hello" # equivalent to "github:nixos/nixpkgs/nixos-22.05#hello" "#hello" # equivalent to "github:nixos/nixpkgs/nixos-22.05#hello"
] ]
command = "hello" command = "hello"
} }
@ -31,7 +34,7 @@ job "nix2-example-batch" {
config { config {
packages = [ packages = [
"curl", "cacert" "#curl", "#cacert"
] ]
command = "curl" command = "curl"
args = [ args = [
@ -42,5 +45,25 @@ job "nix2-example-batch" {
SSL_CERT_FILE = "/etc/ssl/certs/ca-bundle.crt" 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"
}
}
} }
} }

View file

@ -13,17 +13,17 @@ job "nix2-example-service" {
config { config {
packages = [ packages = [
"python3", "#python3",
"bash", "#bash",
"coreutils", "#coreutils",
"curl", "#curl",
"nix", "#nix",
"git", "#git",
"cacert", "#cacert",
"strace", "#strace",
"gnugrep", "#gnugrep",
"findutils", "#findutils",
"mount", "#mount",
] ]
command = "python3" command = "python3"
args = [ "-m", "http.server", "8080" ] args = [ "-m", "http.server", "8080" ]

18
example/flake.nix Normal file
View file

@ -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;
};
}

View file

@ -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. // Use that repo for all packages not specified from a flake already.
for i := range driverConfig.Packages { for i := range driverConfig.Packages {
if !strings.Contains(driverConfig.Packages[i], "#") && !strings.Contains(driverConfig.Packages[i], "/") { if strings.HasPrefix(driverConfig.Packages[i], "#") {
driverConfig.Packages[i] = nixpkgs + "#" + driverConfig.Packages[i] driverConfig.Packages[i] = nixpkgs + driverConfig.Packages[i]
} }
} }

View file

@ -25,7 +25,7 @@ func prepareNixPackages(taskDir string, packages []string, nixpkgs string) (hclu
mounts := make(hclutils.MapStrStr) mounts := make(hclutils.MapStrStr)
profileLink := filepath.Join(taskDir, "current-profile") profileLink := filepath.Join(taskDir, "current-profile")
profile, err := nixBuildProfile(packages, profileLink) profile, err := nixBuildProfile(taskDir, packages, profileLink)
if err != nil { if err != nil {
return nil, fmt.Errorf("Build of the flakes failed: %v", err) 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 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( cmd := exec.Command("nix", append(
[]string{ []string{
"--extra-experimental-features", "nix-command", "--extra-experimental-features", "nix-command",
@ -84,6 +84,7 @@ func nixBuildProfile(flakes []string, link string) (string, error) {
flakes...)...) flakes...)...)
stderr := &bytes.Buffer{} stderr := &bytes.Buffer{}
cmd.Stderr = stderr cmd.Stderr = stderr
cmd.Dir = taskDir
if err := cmd.Run(); err != nil { if err := cmd.Run(); err != nil {
return "", fmt.Errorf("%v failed: %s. Err: %v", cmd.Args, stderr.String(), err) return "", fmt.Errorf("%v failed: %s. Err: %v", cmd.Args, stderr.String(), err)