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 =
{ ... }: { ... }:
{ {
_class = "fediversity-resource-request";
options = {
wheel = mkOption {
description = "Whether the login user needs root permissions";
type = types.bool;
default = false;
};
packages = mkOption {
description = "Packages that need to be available in the user environment";
type = with types; attrsOf package;
};
};
};
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: { implementation = requests: {
input = requests; input = requests;
output.ssh-host = { output.ssh-host = {
ssh = { ssh = {
host = "localhost";
username = "root"; username = "root";
authentication.password = "password"; inherit (deployment-config) host;
key-file = null;
}; };
nixos-configuration = nixos-configuration =
{ ... }: { ... }:
{ {
users.users = config.resources.shell.login-shell.apply ( imports = [
lib.filterAttrs (_name: value: value ? login-shell) requests ./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,7 +39,26 @@ in
extraTestScript = '' extraTestScript = ''
${lib.concatStringsSep "\n" ( ${lib.concatStringsSep "\n" (
lib.lists.map (nodeName: '' lib.lists.map (
nodeName:
let
deployment-config = {
inherit (config) enableAcme;
acmeNodeIP = if config.enableAcme then config.nodes.acme.networking.primaryIPAddress else null;
host = nodeName;
};
inherit
((import ./deployment.nix {
inherit (pkgs) system;
config = deployment-config;
}).ssh-host.ssh
)
host
username
key-file
;
in
''
with subtest("Check the status before deployment"): with subtest("Check the status before deployment"):
${nodeName}.fail("${nodeName} 1>&2") ${nodeName}.fail("${nodeName} 1>&2")
@ -42,33 +67,32 @@ in
set -euo pipefail set -euo pipefail
# INSTANTIATE # INSTANTIATE
command=(nix-instantiate --expr ' command=(nix-instantiate --show-trace --expr '
let let
configuration = { pkgs, config, ... }: { system = "${pkgs.system}"; # FIXME: what system are we deploying to?
imports = [
${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 in
import ${pathToRoot}/deployment/nixos.nix { inherit configuration; } 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 # DEPLOY
host="root@${nodeName}" host="${lib.defaultTo "root" username}@${host}"
sshOpts=( sshOpts=(
${if key-file == null then "" else "-i ${key-file}"}
-o StrictHostKeyChecking=no -o StrictHostKeyChecking=no
-o "ConnectTimeout=1" -o "ConnectTimeout=1"
-o "ServerAliveInterval=1" -o "ServerAliveInterval=1"
) )
# instantiate the config in /nix/store # instantiate the config in /nix/store
"''${command[@]}" -A out_path "''${command[@]}" --show-trace -A out_path
# get the realized derivation to deploy # get the realized derivation to deploy
outPath=$(nix-store --realize "$("''${command[@]}" --eval --strict --json | jq -r '.drv_path')") outPath=$(nix-store --realize "$("''${command[@]}" --show-trace --eval --strict --json | jq -r '.drv_path')")
# deploy the config by nix-copy-closure # deploy the config by nix-copy-closure
NIX_SSHOPTS="''${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes NIX_SSHOPTS="''${sshOpts[*]}" nix-copy-closure --to "$host" "$outPath" --gzip --use-substitutes
# switch the remote host to the config # switch the remote host to the config
@ -82,8 +106,9 @@ in
fi fi
""") """)
${nodeName}.wait_for_unit("multi-user.target") ${nodeName}.wait_for_unit("multi-user.target")
${nodeName}.succeed("${nodeName} 1>&2") ${nodeName}.succeed("su - operator -c ${nodeName} 1>&2")
'') targetMachines ''
) 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";
type = attrTag {
private-key = mkOption {
description = "path to the user's SSH private key"; description = "path to the user's SSH private key";
type = str; type = nullOr str;
example = "/root/.ssh/id_ed25519"; example = "/root/.ssh/id_ed25519";
}; };
password = mkOption {
description = "SSH password";
# TODO: mark as sensitive
type = str;
}; };
}; };
}; };
}; deployment-type = attrTag {
}; ssh-host = mkOption {
};
deployment = attrTag {
ssh-host = {
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 = {