Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't require using a file to pass nodes to build a derivation with all the system in a grid. #186

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions data/eval-machines.nix
Original file line number Diff line number Diff line change
@@ -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 <nixpkgs/lib>);
evalConfig = network.network.evalConfig or ((nwPkgs.path or <nixpkgs>) + "/nixos/lib/eval-config.nix");
runCommand = network.network.runCommand or nwPkgs.runCommand or ((import <nixpkgs> {}).runCommand);
Expand Down Expand Up @@ -101,10 +101,22 @@ in rec {
};

# Phase 2: build complete machine configurations.
machines = { argsFile, buildTargets ? null }:
machines = {
names ? null,
buildTargets ? null,
argsJSON ? null,
}:
assert argsJSON != 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 argsJSON).names
else
names;
Comment on lines +112 to +115
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be nice to not need this logic. I filed NixOS/nix#6059 to be able to remove it.

nodes' = if names != null then
filterAttrs (n: v: elem n fileArgs.Names) nodes
else
nodes;
in
runCommand "morph"
{ preferLocalBuild = true; }
(if buildTargets == null
Expand Down
52 changes: 29 additions & 23 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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
18 changes: 5 additions & 13 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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",
}
Expand Down