From b2adc5926ef44b951b74cbcda4653b7ea8a8491e Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Thu, 2 Sep 2021 21:25:26 -0600 Subject: [PATCH 1/4] Expose eval as nix attribute. --- data/eval-machines.nix | 4 ++-- default.nix | 52 +++++++++++++++++++++++------------------- 2 files changed, 31 insertions(+), 25 deletions(-) diff --git a/data/eval-machines.nix b/data/eval-machines.nix index a4bcda6..8cba819 100644 --- a/data/eval-machines.nix +++ b/data/eval-machines.nix @@ -1,9 +1,9 @@ # Completely stripped down version of nixops' evaluator -{ networkExpr }: +{ networkExpr, pkgs ? {} }: let network = import networkExpr; - nwPkgs = network.network.pkgs or {}; + nwPkgs = network.network.pkgs or pkgs; lib = network.network.lib or nwPkgs.lib or (import ); evalConfig = network.network.evalConfig or ((nwPkgs.path or ) + "/nixos/lib/eval-config.nix"); runCommand = network.network.runCommand or nwPkgs.runCommand or ((import {}).runCommand); diff --git a/default.nix b/default.nix index 58d97bd..fcbfb5b 100644 --- a/default.nix +++ b/default.nix @@ -3,35 +3,41 @@ , version ? "dev" }: -pkgs.buildGoModule rec { - name = "morph-unstable-${version}"; - inherit version; +let + morph = pkgs.buildGoModule rec { + name = "morph-unstable-${version}"; + inherit version; - nativeBuildInputs = with pkgs; [ go-bindata ]; + nativeBuildInputs = with pkgs; [ go-bindata ]; - src = pkgs.nix-gitignore.gitignoreSource [] ./.; + src = pkgs.nix-gitignore.gitignoreSource [] ./.; - buildFlagsArray = '' - -ldflags= - -X - main.version=${version} - ''; + buildFlagsArray = '' + -ldflags= + -X + main.version=${version} + ''; - vendorSha256 = "08zzp0h4c4i5hk4whz06a3da7qjms6lr36596vxz0d8q0n7rspr9"; + vendorSha256 = "08zzp0h4c4i5hk4whz06a3da7qjms6lr36596vxz0d8q0n7rspr9"; - postPatch = '' - go-bindata -pkg assets -o assets/assets.go data/ - ''; + postPatch = '' + go-bindata -pkg assets -o assets/assets.go data/ + ''; - postInstall = '' - mkdir -p $lib - cp -v ./data/*.nix $lib - ''; + postInstall = '' + mkdir -p $lib + cp -v ./data/*.nix $lib + ''; - outputs = [ "out" "lib" ]; + outputs = [ "out" "lib" ]; - meta = { - homepage = "https://github.com/DBCDK/morph"; - description = "Morph is a NixOS host manager written in Golang."; + passthru = { + eval = args@{...}: (import (morph.lib + "/eval-machines.nix")) ({ inherit pkgs; } // args); + }; + + meta = { + homepage = "https://github.com/DBCDK/morph"; + description = "Morph is a NixOS host manager written in Golang."; + }; }; -} +in morph From c45fe802a35e9fa13c77fcebea4eb08d164351bc Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Mon, 7 Feb 2022 13:01:15 -0700 Subject: [PATCH 2/4] Don't require creating a file on disk to build a derivation with all machines. --- data/eval-machines.nix | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/data/eval-machines.nix b/data/eval-machines.nix index 8cba819..8297811 100644 --- a/data/eval-machines.nix +++ b/data/eval-machines.nix @@ -101,10 +101,22 @@ in rec { }; # Phase 2: build complete machine configurations. - machines = { argsFile, buildTargets ? null }: + machines = { + argsFile ? null, + names ? null + buildTargets ? null + }: + assert argsFile != null -> names == null; let - fileArgs = builtins.fromJSON (builtins.readFile argsFile); - nodes' = filterAttrs (n: v: elem n fileArgs.Names) nodes; in + names' = if argsFile != null then + (builtins.fromJSON (builtins.readFile argsFile)).Names + else + names; + nodes' = if names != null then + filterAttrs (n: v: elem n fileArgs.Names) nodes + else + nodes; + in runCommand "morph" { preferLocalBuild = true; } (if buildTargets == null From 93205ebe8c9e2c5b74e77a0e267a85183b50311a Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Mon, 7 Feb 2022 13:09:44 -0700 Subject: [PATCH 3/4] Don't use a file on disk to pass target nodes from go to nix. --- data/eval-machines.nix | 10 +++++----- nix/nix.go | 18 +++++------------- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/data/eval-machines.nix b/data/eval-machines.nix index 8297811..177f3eb 100644 --- a/data/eval-machines.nix +++ b/data/eval-machines.nix @@ -102,14 +102,14 @@ in rec { # Phase 2: build complete machine configurations. machines = { - argsFile ? null, - names ? null - buildTargets ? null + names ? null, + buildTargets ? null, + argsJSON ? null, }: - assert argsFile != null -> names == null; + assert argsJSON != null -> names == null; let names' = if argsFile != null then - (builtins.fromJSON (builtins.readFile argsFile)).Names + (builtins.fromJSON argsJSON).names else names; nodes' = if names != null then diff --git a/nix/nix.go b/nix/nix.go index 274516b..9674c1f 100644 --- a/nix/nix.go +++ b/nix/nix.go @@ -54,8 +54,8 @@ type NixContext struct { AllowBuildShell bool } -type FileArgs struct { - Names []string +type JSONArgs struct { + Names []string `json:"names"` } func (host *Host) GetName() string { @@ -228,17 +228,9 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg hostsArg = append(hostsArg, host.Name) } - fileArgs := FileArgs{ + jsonArgs, err := json.Marshal(JSONArgs{ Names: hostsArg, - } - - jsonArgs, err := json.Marshal(fileArgs) - if err != nil { - return "", err - } - argsFile := tmpdir + "/morph-args.json" - - err = ioutil.WriteFile(argsFile, jsonArgs, 0644) + }) if err != nil { return "", err } @@ -259,7 +251,7 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg "-f", ctx.EvalMachines, "-v", "--arg", "networkExpr", deploymentPath, - "--argstr", "argsFile", argsFile, + "--argstr", "argsJSON", string(jsonArgs), "--out-link", resultLinkPath, "machines", } From 3a55bd869dfa62828d37a9653cbabf4b36d429b4 Mon Sep 17 00:00:00 2001 From: Tom Prince Date: Mon, 7 Feb 2022 14:52:17 -0700 Subject: [PATCH 4/4] Allow passing evaluated expr. --- data/eval-machines.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/data/eval-machines.nix b/data/eval-machines.nix index 177f3eb..a1ed03e 100644 --- a/data/eval-machines.nix +++ b/data/eval-machines.nix @@ -2,7 +2,8 @@ { networkExpr, pkgs ? {} }: let - network = import networkExpr; + isImportable = v: builtins.isString v || builtins.isPath v || v ? outPath || v ? __toString; + network = if isImportable networkExpr then import networkExpr else networkExpr; nwPkgs = network.network.pkgs or pkgs; lib = network.network.lib or nwPkgs.lib or (import ); evalConfig = network.network.evalConfig or ((nwPkgs.path or ) + "/nixos/lib/eval-config.nix");