Skip to content

Commit

Permalink
addded support for optional nixConfig attrset on network-level
Browse files Browse the repository at this point in the history
  • Loading branch information
Johan Thomsen authored and adamtulinius committed Oct 27, 2019
1 parent 83bf6d4 commit 86f1760
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 3 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,33 @@ Health checks will be repeated until success, and the interval can be configured

It is currently possible to have expressions like `"test \"$(systemctl list-units --failed --no-legend --no-pager |wc -l)\" -eq 0"` (count number of failed systemd units, fail if non-zero) as the first argument in a cmd-healthcheck. This works, but is discouraged, and might break at any time.

### Advanced configuration

**nix.conf-options:** The "network"-attrset supports a sub-attrset named "nixConfig". Options configured here will pass `--option <name> <value>` to all nix commands.
Note: these options apply to an entire deployment and are *not* configurable on per-host basis.
The default is an empty set, meaning that the nix configuration is inherited from the build environment. See `man nix.conf`.

**special deployment options:**

(per-host granularity)

`buildOnly` makes morph skip the "push" and "switch" steps for the given host, even if "morph deploy" or "morph push" is executed. (default: false)


Example usage of `nixConfig` and `deployment.buildOnly`:
```
network = {
nixConfig = {
"extra-sandbox-paths" = "/foo/bar";
};
};
machine1 = { ... }: {
deployment.buildOnly = true;
};
```


## Hacking morph

Expand Down
3 changes: 3 additions & 0 deletions data/eval-machines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ rec {
{ inherit (v.config.deployment) targetHost secrets healthChecks buildOnly;
name = n;
nixosRelease = v.config.system.nixos.release or (removeSuffix v.config.system.nixos.version.suffix v.config.system.nixos.version);
nixConfig = mapAttrs
(n: v: if builtins.isString v then v else throw "nix option '${n}' must have a string typed value")
(network'.network.nixConfig or {});
}
);

Expand Down
25 changes: 22 additions & 3 deletions nix/nix.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Host struct {
TargetHost string
Secrets map[string]secrets.Secret
BuildOnly bool
NixConfig map[string]string
}

type NixContext struct {
Expand Down Expand Up @@ -162,6 +163,8 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg
"--arg", "names", hostsArg,
"--out-link", resultLinkPath}

args = append(args, mkOptions(hosts[0])...)

if len(nixArgs) > 0 {
args = append(args, nixArgs...)
}
Expand Down Expand Up @@ -200,6 +203,16 @@ func (ctx *NixContext) BuildMachines(deploymentPath string, hosts []Host, nixArg
return
}

func mkOptions(host Host) []string {
var options = make([]string, 0)
for k, v := range host.NixConfig {
options = append(options, "--option")
options = append(options, k)
options = append(options, v)
}
return options
}

func GetNixSystemPath(host Host, resultPath string) (string, error) {
return os.Readlink(filepath.Join(resultPath, host.Name))
}
Expand Down Expand Up @@ -238,11 +251,17 @@ func Push(ctx *ssh.SSHContext, host Host, paths ...string) (err error) {
env = append(env, fmt.Sprintf("NIX_SSHOPTS=%s","-o StrictHostkeyChecking=No -o UserKnownHostsFile=/dev/null"))
}

options := mkOptions(host)
for _, path := range paths {
cmd := exec.Command(
"nix", "copy",
args := []string{
"copy",
path,
"--to", "ssh://"+userArg+host.TargetHost+keyArg,
"--to", "ssh://" + userArg + host.TargetHost + keyArg,
}
args = append(args, options...)

cmd := exec.Command(
"nix", args...,
)
cmd.Env = env

Expand Down

0 comments on commit 86f1760

Please sign in to comment.