Compare commits

..

13 commits

6 changed files with 233 additions and 101 deletions

View file

@ -1,11 +1,17 @@
{ {
inputs,
lib,
config, config,
system,
inputs ? (import ../../../default.nix { }).inputs,
sources ? import ../../../npins,
... ...
}: }:
let let
inherit (sources) nixpkgs;
pkgs = import nixpkgs { inherit system; };
inherit (pkgs) lib;
deployment-config = config;
inherit (lib) mkOption types;
eval = eval =
module: module:
(lib.evalModules { (lib.evalModules {
@ -18,34 +24,128 @@ let
]; ];
}).config; }).config;
fediversity = eval ( fediversity = eval (
{ ... }: { config, ... }:
{ {
config = { config = {
environments.single-nixos-vm = resources.login-shell = {
{ ... }: description = "The operator needs to be able to log into the shell";
{ request =
implementation = requests: { { ... }:
input = requests; {
output.ssh-host = { _class = "fediversity-resource-request";
ssh = { options = {
host = "localhost"; wheel = mkOption {
username = "root"; description = "Whether the login user needs root permissions";
authentication.password = "password"; type = types.bool;
default = false;
}; };
nixos-configuration = packages = mkOption {
{ ... }: description = "Packages that need to be available in the user environment";
{ type = with types; attrsOf package;
users.users = config.resources.shell.login-shell.apply ( };
lib.filterAttrs (_name: value: value ? login-shell) requests };
); };
policy =
{ config, ... }:
{
_class = "fediversity-resource-policy";
options = {
username = mkOption {
description = "Username for the operator";
type = types.str; # TODO: use the proper constraints from NixOS
};
wheel = mkOption {
description = "Whether to allow login with root permissions";
type = types.bool;
default = false;
};
};
config = {
resource-type = types.raw; # TODO: splice out the user type from NixOS
apply =
requests:
let
# Filter out requests that need wheel if policy doesn't allow it
validRequests = lib.filterAttrs (
_name: req: !req.login-shell.wheel || config.wheel
) requests.resources;
in
lib.optionalAttrs (validRequests != { }) {
${config.username} = {
isNormalUser = true;
packages =
with lib;
attrValues (concatMapAttrs (_name: request: request.login-shell.packages) validRequests);
extraGroups = lib.optional config.wheel "wheel";
};
}; };
}; };
}; };
};
applications.hello =
{ ... }:
{
description = ''Command-line tool that will print "Hello, world!" on the terminal'';
module =
{ ... }:
{
options.enable = lib.mkEnableOption "Hello in the shell";
};
implementation = cfg: {
input = cfg;
output = lib.optionalAttrs cfg.enable {
resources.hello.login-shell.packages.hello = pkgs.hello;
};
};
}; };
environments.single-nixos-vm = environment: {
resources."operator-environment".login-shell.username = "operator";
implementation = requests: {
input = requests;
output.ssh-host = {
ssh = {
username = "root";
inherit (deployment-config) host;
key-file = null;
};
nixos-configuration =
{ ... }:
{
imports = [
./options.nix
../common/sharedOptions.nix
../common/targetNode.nix
"${nixpkgs}/nixos/modules/profiles/qemu-guest.nix"
];
inherit (deployment-config) enableAcme;
acmeNodeIP =
if deployment-config.enableAcme then
deployment-config.nodes.acme.networking.primaryIPAddress
else
null;
users.users = environment.config.resources."operator-environment".login-shell.apply {
resources = lib.filterAttrs (_name: value: value ? login-shell) requests;
};
};
};
};
};
};
options = {
"example-configuration" = mkOption {
type = config.configuration;
default = {
enable = true;
applications.hello.enable = true;
};
};
"example-deployment" = mkOption {
default = config.environments.single-nixos-vm.deployment config."example-configuration";
};
}; };
} }
); );
in in
fediversity.environments.single-nixos-vm.deployment { fediversity."example-deployment"
enable = true;
}

View file

@ -1,13 +1,19 @@
{ {
lib, lib,
config, config,
pkgs,
... ...
}: }:
let let
inherit (import ./constants.nix) targetMachines pathToRoot; inherit (import ./constants.nix) targetMachines pathToRoot;
escapedJson = v: lib.replaceStrings [ "\"" ] [ "\\\\\"" ] (lib.strings.toJSON v);
in in
{ {
_class = "nixosTest"; _class = "nixosTest";
imports = [
./options.nix
];
name = "deployment-model"; name = "deployment-model";
sourceFileset = lib.fileset.unions [ sourceFileset = lib.fileset.unions [
../../data-model.nix ../../data-model.nix
@ -33,57 +39,76 @@ in
extraTestScript = '' extraTestScript = ''
${lib.concatStringsSep "\n" ( ${lib.concatStringsSep "\n" (
lib.lists.map (nodeName: '' lib.lists.map (
with subtest("Check the status before deployment"): nodeName:
${nodeName}.fail("${nodeName} 1>&2") let
deployment-config = {
with subtest("Run the deployment for ${nodeName}"): inherit (config) enableAcme;
deployer.succeed(""" acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
set -euo pipefail host = nodeName;
};
# INSTANTIATE inherit
command=(nix-instantiate --expr ' ((import ./deployment.nix {
let inherit (pkgs) system;
configuration = { pkgs, config, ... }: { config = deployment-config;
imports = [ }).ssh-host.ssh
${pathToRoot}/deployment/check/common/sharedOptions.nix
${pathToRoot}/deployment/check/common/targetNode.nix
];
enableAcme = ${lib.strings.toJSON config.enableAcme};
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
environment.systemPackages = with pkgs; [
hello
];
};
in
import ${pathToRoot}/deployment/nixos.nix { inherit configuration; }
')
# DEPLOY
host="root@${nodeName}"
sshOpts=(
-o StrictHostKeyChecking=no
-o "ConnectTimeout=1"
-o "ServerAliveInterval=1"
) )
# instantiate the config in /nix/store host
"''${command[@]}" -A out_path username
# get the realized derivation to deploy key-file
outPath=$(nix-store --realize "$("''${command[@]}" --eval --strict --json | jq -r '.drv_path')") ;
# deploy the config by nix-copy-closure in
NIX_SSHOPTS="''${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes ''
# switch the remote host to the config with subtest("Check the status before deployment"):
output=$(ssh "''${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?" ${nodeName}.fail("${nodeName} 1>&2")
echo "output: $output"
if [[ $output != *"Timeout, server ${nodeName} not responding"* ]]; then with subtest("Run the deployment for ${nodeName}"):
echo "non-timeout error: $output" deployer.succeed("""
exit 1 set -euo pipefail
else
exit 0 # INSTANTIATE
fi command=(nix-instantiate --show-trace --expr '
""") let
${nodeName}.wait_for_unit("multi-user.target") system = "${pkgs.system}"; # FIXME: what system are we deploying to?
${nodeName}.succeed("${nodeName} 1>&2") in
'') targetMachines import ${pathToRoot}/deployment/nixos.nix {
inherit system;
configuration = (
import ${pathToRoot}/deployment/check/data-model/deployment.nix {
inherit system;
config = builtins.fromJSON "${escapedJson deployment-config}";
}
).ssh-host.nixos-configuration;
}
')
# DEPLOY
host="${lib.defaultTo "root" username}@${host}"
sshOpts=(
${if key-file == null then "" else "-i ${key-file}"}
-o StrictHostKeyChecking=no
-o "ConnectTimeout=1"
-o "ServerAliveInterval=1"
)
# instantiate the config in /nix/store
"''${command[@]}" --show-trace -A out_path
# get the realized derivation to deploy
outPath=$(nix-store --realize "$("''${command[@]}" --show-trace --eval --strict --json | jq -r '.drv_path')")
# deploy the config by nix-copy-closure
NIX_SSHOPTS="''${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes
# switch the remote host to the config
output=$(ssh "''${sshOpts[@]}" "$host" "nix-env --profile /nix/var/nix/profiles/system --set $outPath; nohup $outPath/bin/switch-to-configuration switch &" 2>&1) || echo "status code: $?"
echo "output: $output"
if [[ $output != *"Timeout, server ${nodeName} not responding"* ]]; then
echo "non-timeout error: $output"
exit 1
else
exit 0
fi
""")
${nodeName}.wait_for_unit("multi-user.target")
${nodeName}.succeed("su - operator -c ${nodeName} 1>&2")
''
) targetMachines
)} )}
''; '';
} }

View file

@ -0,0 +1,15 @@
{
lib,
...
}:
let
inherit (lib) types;
in
{
options = {
host = lib.mkOption {
type = types.str;
description = "name of the host to deploy to";
};
};
}

View file

@ -108,7 +108,7 @@ in
environments.single-nixos-vm = environments.single-nixos-vm =
{ config, ... }: { config, ... }:
{ {
resources.operator-environment.login-shell.username = "operator"; resources."operator-environment".login-shell.username = "operator";
implementation = requests: { implementation = requests: {
input = requests; input = requests;
output.nixops4 = output.nixops4 =
@ -125,9 +125,9 @@ in
nixos.module = nixos.module =
{ ... }: { ... }:
{ {
users.users = config.resources.shell.login-shell.apply ( users.users = config.resources."operator-environment".login-shell.apply {
lib.filterAttrs (_name: value: value ? login-shell) requests resources = lib.filterAttrs (_name: value: value ? login-shell) requests;
); };
}; };
}; };
}; };
@ -135,7 +135,7 @@ in
}; };
}; };
options = { options = {
example-configuration = mkOption { "example-configuration" = mkOption {
type = config.configuration; type = config.configuration;
readOnly = true; readOnly = true;
default = { default = {
@ -143,20 +143,22 @@ in
applications.hello.enable = true; applications.hello.enable = true;
}; };
}; };
example-deployment = mkOption { "example-deployment" = mkOption {
type = options.deployments.nestedType; type = options.deployments.nestedType;
readOnly = true; readOnly = true;
default = config.environments.single-nixos-vm.deployment config.example-configuration; default = config.environments.single-nixos-vm.deployment config."example-configuration";
}; };
}; };
} }
); );
resources = fediversity.applications.hello.resources fediversity.example-configuration.applications.hello; resources =
fediversity.applications.hello.resources
fediversity."example-configuration".applications.hello;
hello-shell = resources.resources.hello.login-shell; hello-shell = resources.resources.hello.login-shell;
environment = fediversity.environments.single-nixos-vm.resources.operator-environment.login-shell; environment = fediversity.environments.single-nixos-vm.resources."operator-environment".login-shell;
result = mkDeployment { result = mkDeployment {
modules = [ modules = [
(fediversity.environments.single-nixos-vm.deployment fediversity.example-configuration) (fediversity.environments.single-nixos-vm.deployment fediversity."example-configuration")
]; ];
}; };

View file

@ -45,26 +45,16 @@ let
type = nullOr str; type = nullOr str;
default = null; default = null;
}; };
authentication = mkOption { key-file = mkOption {
description = "authentication method"; description = "path to the user's SSH private key";
type = attrTag { type = nullOr str;
private-key = mkOption { example = "/root/.ssh/id_ed25519";
description = "path to the user's SSH private key";
type = str;
example = "/root/.ssh/id_ed25519";
};
password = mkOption {
description = "SSH password";
# TODO: mark as sensitive
type = str;
};
};
}; };
}; };
}; };
}; };
deployment = attrTag { deployment-type = attrTag {
ssh-host = { ssh-host = mkOption {
description = "A Terraform deployment by SSH to update a single existing NixOS host."; description = "A Terraform deployment by SSH to update a single existing NixOS host.";
type = submodule { type = submodule {
options = { options = {
@ -185,7 +175,7 @@ in
readOnly = true; readOnly = true;
default = { default = {
input-type = application-resources; input-type = application-resources;
output-type = deployment; output-type = deployment-type;
}; };
}; };
# TODO(@fricklerhandwerk): maybe this should be a separate thing such as `fediversity-setup`, # TODO(@fricklerhandwerk): maybe this should be a separate thing such as `fediversity-setup`,

View file

@ -1,9 +1,9 @@
{ {
configuration, configuration,
system ? builtins.currentSystem, system,
sources ? import ../npins,
}: }:
let let
sources = import ../npins;
eval = import "${sources.nixpkgs}/nixos/lib/eval-config.nix" { eval = import "${sources.nixpkgs}/nixos/lib/eval-config.nix" {
inherit system; inherit system;
specialArgs = { specialArgs = {