Skip to content

Commit

Permalink
Merge pull request #221 from DBCDK/fix-formatting
Browse files Browse the repository at this point in the history
we're using the wrong formatter! (and other formatting issues)
  • Loading branch information
srhb authored Sep 17, 2024
2 parents 1176d42 + d1657ba commit d13c3d6
Show file tree
Hide file tree
Showing 12 changed files with 225 additions and 129 deletions.
195 changes: 114 additions & 81 deletions data/eval-machines.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ let
network = import networkExpr;
nwPkgs = network.network.pkgs or { };
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;
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;
in
with lib;

let
defaults = network.defaults or { };

modules = { machineName, nodes, check }:
modules =
{
machineName,
nodes,
check,
}:
[
# Get the configuration of this machine from each network
# expression, attaching _file attributes so the NixOS module
Expand All @@ -25,47 +28,53 @@ let

defaults

({ lib, ... }: {
key = "deploy-stuff";
imports = [ ./options.nix ];
# Make documentation builds deterministic, even with our
# tempdir module imports.
documentation.nixos.extraModuleSources = [ ../. ];
# Provide a default hostname and deployment target equal
# to the attribute name of the machine in the model.
networking.hostName = lib.mkDefault machineName;
deployment.targetHost = lib.mkDefault machineName;

# If network.pkgs is set, mkDefault nixpkgs.pkgs
nixpkgs.pkgs = lib.mkIf (nwPkgs != { }) (lib.mkDefault nwPkgs);

# Avoid the deprecated evalConfig arguments by
# setting them here instead.
_module = {
args = {
name = machineName;
inherit nodes;
(
{ lib, ... }:
{
key = "deploy-stuff";
imports = [ ./options.nix ];
# Make documentation builds deterministic, even with our
# tempdir module imports.
documentation.nixos.extraModuleSources = [ ../. ];
# Provide a default hostname and deployment target equal
# to the attribute name of the machine in the model.
networking.hostName = lib.mkDefault machineName;
deployment.targetHost = lib.mkDefault machineName;

# If network.pkgs is set, mkDefault nixpkgs.pkgs
nixpkgs.pkgs = lib.mkIf (nwPkgs != { }) (lib.mkDefault nwPkgs);

# Avoid the deprecated evalConfig arguments by
# setting them here instead.
_module = {
args = {
name = machineName;
inherit nodes;
};
inherit check;
};
inherit check;
};
})
] ++ optional (network ? _file) { inherit (network) _file; };

machineNames = attrNames (removeAttrs network [
"network"
"defaults"
"resources"
"require"
"_file"
]);
}
)
]
++ optional (network ? _file) { inherit (network) _file; };

machineNames = attrNames (
removeAttrs network [
"network"
"defaults"
"resources"
"require"
"_file"
]
);

in
rec {
# Unchecked configuration of all machines.
# Using unchecked config evaluation allows each machine to access other machines
# configuration without recursing as full evaluation is prevented
uncheckedNodes = listToAttrs (map
(machineName: {
uncheckedNodes = listToAttrs (
map (machineName: {
name = machineName;
value = import evalConfig {
# Force decide system in module system
Expand All @@ -76,12 +85,12 @@ rec {
nodes = uncheckedNodes;
};
};
})
machineNames);
}) machineNames
);

# Compute the definitions of the machines.
nodes = listToAttrs (map
(machineName: {
nodes = listToAttrs (
map (machineName: {
name = machineName;
value = import evalConfig {
# Force decide system in module system
Expand All @@ -92,8 +101,8 @@ rec {
nodes = uncheckedNodes;
};
};
})
machineNames);
}) machineNames
);

deploymentInfoModule = {
deployment = {
Expand All @@ -105,27 +114,37 @@ rec {

# Phase 1: evaluate only the deployment attributes.
info =
let network' = network;
in rec {

machines = flip mapAttrs nodes (n: v':
let v = scrubOptionValue v';
in {
let
network' = network;
in
rec {

machines = flip mapAttrs nodes (
n: v':
let
v = scrubOptionValue v';
in
{
inherit (v.config.deployment)
targetHost targetPort targetUser secrets preDeployChecks healthChecks buildOnly
substituteOnDestination tags;
targetHost
targetPort
targetUser
secrets
preDeployChecks
healthChecks
buildOnly
substituteOnDestination
tags
;
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 { });
});
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 { });
}
);

machineList = map (key: getAttr key machines) (attrNames machines);
network = network'.network or { };
Expand All @@ -141,25 +160,39 @@ rec {
};

# Phase 2: build complete machine configurations.
machines = { argsFile, buildTargets ? null }:
machines =
{
argsFile,
buildTargets ? null,
}:
let
fileArgs = builtins.fromJSON (builtins.readFile argsFile);
nodes' = filterAttrs (n: _v: elem n fileArgs.Names) nodes;
in
runCommand "morph" { preferLocalBuild = true; }
(if buildTargets == null then ''
mkdir -p $out
${toString (mapAttrsToList (nodeName: nodeDef: ''
ln -s ${nodeDef.config.system.build.toplevel} $out/${nodeName}
'') nodes')}
'' else ''
mkdir -p $out
${toString (mapAttrsToList (nodeName: nodeDef: ''
mkdir -p $out/${nodeName}
${toString (mapAttrsToList (buildName: buildFn: ''
ln -s ${buildFn nodeDef} $out/${nodeName}/${buildName}
'') buildTargets)}
'') nodes')}
'');
runCommand "morph" { preferLocalBuild = true; } (
if buildTargets == null then
''
mkdir -p $out
${toString (
mapAttrsToList (nodeName: nodeDef: ''
ln -s ${nodeDef.config.system.build.toplevel} $out/${nodeName}
'') nodes'
)}
''
else
''
mkdir -p $out
${toString (
mapAttrsToList (nodeName: nodeDef: ''
mkdir -p $out/${nodeName}
${toString (
mapAttrsToList (buildName: buildFn: ''
ln -s ${buildFn nodeDef} $out/${nodeName}/${buildName}
'') buildTargets
)}
'') nodes'
)}
''
);

}
19 changes: 14 additions & 5 deletions data/options.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ config, lib, pkgs, ... }:
{
config,
lib,
pkgs,
...
}:

with lib;
with lib.types;
Expand Down Expand Up @@ -50,8 +55,7 @@ let
action = mkOption {
default = [ ];
type = listOf str;
description =
"Action to perform on remote host after uploading secret.";
description = "Action to perform on remote host after uploading secret.";
};

mkDirs = mkOption {
Expand All @@ -66,7 +70,10 @@ let

uploadAt = mkOption {
default = "pre-activation";
type = enum [ "pre-activation" "post-activation" ];
type = enum [
"pre-activation"
"post-activation"
];
description = ''
When to upload the secret.
Expand Down Expand Up @@ -268,7 +275,9 @@ in
# all derived dependencies.
config.system.extraDependencies =
let
cmds = concatMap (h: h.cmd) (config.deployment.preDeployChecks.cmd ++ config.deployment.healthChecks.cmd);
cmds = concatMap (h: h.cmd) (
config.deployment.preDeployChecks.cmd ++ config.deployment.healthChecks.cmd
);
in
[ (pkgs.writeText "healthcheck-commands.txt" (concatStringsSep "\n" cmds)) ];
}
17 changes: 13 additions & 4 deletions default.nix
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
{ nixpkgs ? import ./nixpkgs.nix, pkgs ? import nixpkgs { }, version ? "dev" }:
{
nixpkgs ? import ./nixpkgs.nix,
pkgs ? import nixpkgs { },
version ? "dev",
}:

pkgs.buildGoModule rec {
name = "morph-unstable-${version}";
inherit version;

src = pkgs.nix-gitignore.gitignoreSource [ ] ./.;

ldflags =
[ "-X main.version=${version}" "-X main.assetRoot=${placeholder "lib"}" ];
ldflags = [
"-X main.version=${version}"
"-X main.assetRoot=${placeholder "lib"}"
];

vendorHash = "sha256-zQlMtbXgrH83zrcIoOuFhb2tYCeQ1pz4UQUvRIsLMCE==";

Expand All @@ -16,7 +22,10 @@ pkgs.buildGoModule rec {
cp -v ./data/*.nix $lib
'';

outputs = [ "out" "lib" ];
outputs = [
"out"
"lib"
];

meta = {
homepage = "https://github.com/DBCDK/morph";
Expand Down
23 changes: 15 additions & 8 deletions examples/healthchecks.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
let pkgs = import (import ../nixpkgs.nix) { };
in {
let
pkgs = import (import ../nixpkgs.nix) { };
in
{
network = {
inherit pkgs;
description = "health check demo hosts";
Expand All @@ -24,10 +26,16 @@ in {

deployment = {
healthChecks = {
cmd = [{
cmd = [ "true" "one argument" "another argument" ];
description = "Testing that 'true' works.";
}];
cmd = [
{
cmd = [
"true"
"one argument"
"another argument"
];
description = "Testing that 'true' works.";
}
];

http = [
{
Expand All @@ -40,8 +48,7 @@ in {
{
scheme = "https";
port = 443;
host =
"some-other-host.example.com"; # defaults to the hostname of the host if unset
host = "some-other-host.example.com"; # defaults to the hostname of the host if unset
path = "/health";
description = "Check whether $imaginaryService is running.";
}
Expand Down
Loading

0 comments on commit d13c3d6

Please sign in to comment.