Misc cleanup of nix files #15

Open
thubrecht wants to merge 5 commits from cleanup into main
39 changed files with 1562 additions and 1156 deletions

View file

@ -1,3 +1 @@
let
flake = import ./flake-compat.nix;
in flake.defaultNix.default
(import ./flake-compat.nix).defaultNix.default

View file

@ -1,9 +1,7 @@
let
lock = builtins.fromJSON (builtins.readFile ./flake.lock);
flakeCompat = import (fetchTarball {
in
import (builtins.fetchTarball {
url = "https://github.com/edolstra/flake-compat/archive/${lock.nodes.flake-compat.locked.rev}.tar.gz";
sha256 = lock.nodes.flake-compat.locked.narHash;
});
in flakeCompat {
src = ./.;
}
}) { src = ./.; }

120
flake.nix
View file

@ -18,32 +18,41 @@
};
};
outputs = {
outputs =
{
self,
nixpkgs,
stable,
flake-utils,
nix-github-actions,
...
} @ inputs: let
supportedSystems = [ "x86_64-linux" "i686-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
colmenaOptions = import ./src/nix/hive/options.nix;
colmenaModules = import ./src/nix/hive/modules.nix;
in flake-utils.lib.eachSystem supportedSystems (system: let
pkgs = import nixpkgs {
inherit system;
overlays = [
}@inputs:
let
supportedSystems = [
"x86_64-linux"
"i686-linux"
"aarch64-linux"
"x86_64-darwin"
"aarch64-darwin"
];
};
in rec {
in
flake-utils.lib.eachSystem supportedSystems (
system:
let
pkgs = import nixpkgs { inherit system; };
in
rec {
# We still maintain the expression in a Nixpkgs-acceptable form
defaultPackage = self.packages.${system}.colmena;
packages = rec {
colmena = pkgs.callPackage ./package.nix { };
# Full user manual
manual = let
suppressModuleArgsDocs = { lib, ... }: {
manual =
let
suppressModuleArgsDocs =
{ lib, ... }:
{
options = {
_module.args = lib.mkOption {
internal = true;
@ -51,18 +60,36 @@
};
};
colmena = self.packages.${system}.colmena;
deploymentOptionsMd = (pkgs.nixosOptionsDoc {
inherit (pkgs.lib.evalModules {
modules = [ colmenaOptions.deploymentOptions suppressModuleArgsDocs];
specialArgs = { name = "nixos"; nodes = {}; };
}) options;
deploymentOptionsMd =
(pkgs.nixosOptionsDoc {
inherit
(pkgs.lib.evalModules {
modules = [
./src/nix/hive/options/deployment.nix
suppressModuleArgsDocs
];
specialArgs = {
name = "nixos";
nodes = { };
};
})
options
;
}).optionsCommonMark;
metaOptionsMd = (pkgs.nixosOptionsDoc {
inherit (pkgs.lib.evalModules {
modules = [ colmenaOptions.metaOptions suppressModuleArgsDocs];
}) options;
metaOptionsMd =
(pkgs.nixosOptionsDoc {
inherit
(pkgs.lib.evalModules {
modules = [
./src/nix/hive/options/meta.nix
suppressModuleArgsDocs
];
})
options
;
}).optionsCommonMark;
in pkgs.callPackage ./manual {
in
pkgs.callPackage ./manual {
inherit colmena deploymentOptionsMd metaOptionsMd;
};
@ -77,26 +104,37 @@
apps.default = self.apps.${system}.colmena;
apps.colmena = {
type = "app";
program = "${defaultPackage}/bin/colmena";
program = pkgs.lib.getExe defaultPackage;
};
devShell = pkgs.mkShell {
RUST_SRC_PATH = "${pkgs.rustPlatform.rustcSrc}/library";
RUST_SRC_PATH = pkgs.rustPlatform.rustLibSrc;
NIX_PATH = "nixpkgs=${pkgs.path}";
inputsFrom = [ defaultPackage packages.manualFast ];
inputsFrom = [
defaultPackage
packages.manualFast
];
packages = with pkgs; [
bashInteractive
editorconfig-checker
clippy rust-analyzer cargo-outdated cargo-audit rustfmt
python3 python3Packages.flake8
clippy
rust-analyzer
cargo-outdated
cargo-audit
rustfmt
python3
python3Packages.flake8
];
};
checks = let
checks =
let
inputsOverlay = final: prev: {
_inputs = inputs;
};
in if pkgs.stdenv.isLinux then import ./integration-tests {
in
if pkgs.stdenv.isLinux then
import ./integration-tests {
pkgs = import nixpkgs {
inherit system;
overlays = [
@ -111,19 +149,29 @@
inputsOverlay
];
};
} else {};
}) // {
}
else
{ };
}
)
// {
overlay = self.overlays.default;
overlays.default = final: prev: {
colmena = final.callPackage ./package.nix { };
};
nixosModules = {
inherit (colmenaOptions) deploymentOptions metaOptions;
inherit (colmenaModules) keyChownModule keyServiceModule assertionModule;
deploymentOptions = import ./src/nix/hive/options/deployment.nix;
metaOptions = import ./src/nix/hive/options/meta.nix;
keyChownModule = import ./src/nix/hive/modules/key-chown.nix;
keyServiceModule = import ./src/nix/hive/modules/key-service.nix;
assertionModule = import ./src/nix/hive/modules/assertions.nix;
};
lib.makeHive = rawHive: import ./src/nix/hive/eval.nix {
inherit rawHive colmenaOptions colmenaModules;
lib.makeHive =
rawHive:
import ./src/nix/hive/eval.nix {
inherit rawHive;
hermetic = true;
};

View file

@ -4,7 +4,8 @@ let
tools = pkgs.callPackage ../tools.nix {
targets = [ "alpha" ];
};
in tools.runTest {
in
tools.runTest {
name = "colmena-allow-apply-all";
colmena.test = {

View file

@ -3,7 +3,8 @@ let
insideVm = true;
targets = [ "alpha" ];
};
in {
in
{
meta = {
nixpkgs = tools.pkgs;
allowApplyAll = false;

View file

@ -12,7 +12,8 @@ let
security.sudo.wheelNeedsPassword = false;
};
};
in tools.runTest {
in
tools.runTest {
name = "colmena-apply-local";
colmena.test = {

View file

@ -4,12 +4,15 @@ let
targets = [ ];
prebuiltTarget = "deployer";
};
in {
in
{
meta = {
nixpkgs = tools.pkgs;
};
deployer = { lib, ... }: {
deployer =
{ lib, ... }:
{
imports = [
(tools.getStandaloneConfigFor "deployer")
];

View file

@ -1,17 +1,21 @@
{ pkgs
, evaluator ? "chunked"
{
pkgs,
evaluator ? "chunked",
}:
let
tools = pkgs.callPackage ../tools.nix { };
in tools.runTest {
in
tools.runTest {
name = "colmena-apply-${evaluator}";
colmena.test = {
bundle = ./.;
testScript = ''
testScript =
''
colmena = "${tools.colmenaExec}"
evaluator = "${evaluator}"
'' + builtins.readFile ./test-script.py;
''
+ builtins.readFile ./test-script.py;
};
}

View file

@ -1,13 +1,16 @@
let
tools = import ./tools.nix { insideVm = true; };
testPkg = let
testPkg =
let
text = builtins.trace "must appear during evaluation" ''
echo "must appear during build"
mkdir -p $out
'';
in tools.pkgs.runCommand "test-package" {} text;
in {
in
tools.pkgs.runCommand "test-package" { } text;
in
{
meta = {
nixpkgs = tools.pkgs;
};
@ -72,7 +75,9 @@ in {
};
};
alpha = { lib, ... }: {
alpha =
{ lib, ... }:
{
imports = [
(tools.getStandaloneConfigFor "alpha")
];
@ -90,7 +95,9 @@ in {
beta = tools.getStandaloneConfigFor "beta";
gamma = tools.getStandaloneConfigFor "gamma";
"gamma.tld" = { lib, ... }: {
"gamma.tld" =
{ lib, ... }:
{
imports = [
(tools.getStandaloneConfigFor "gamma")
];

View file

@ -2,10 +2,15 @@
let
tools = pkgs.callPackage ../tools.nix {
deployers = [ "deployer" "alpha" "beta" ];
deployers = [
"deployer"
"alpha"
"beta"
];
targets = [ ];
};
in tools.runTest {
in
tools.runTest {
name = "colmena-build-on-target";
colmena.test = {

View file

@ -1,10 +1,15 @@
let
tools = import ./tools.nix {
insideVm = true;
deployers = [ "deployer" "alpha" "beta" ];
deployers = [
"deployer"
"alpha"
"beta"
];
targets = [ ];
};
in {
in
{
meta = {
nixpkgs = tools.pkgs;
};

View file

@ -1,10 +1,14 @@
{ pkgs ? import ./nixpkgs.nix
, pkgsStable ? import ./nixpkgs-stable.nix
{
pkgs ? import ./nixpkgs.nix,
pkgsStable ? import ./nixpkgs-stable.nix,
}:
{
apply = import ./apply { inherit pkgs; };
apply-streaming = import ./apply { inherit pkgs; evaluator = "streaming"; };
apply-streaming = import ./apply {
inherit pkgs;
evaluator = "streaming";
};
apply-local = import ./apply-local { inherit pkgs; };
build-on-target = import ./build-on-target { inherit pkgs; };
exec = import ./exec { inherit pkgs; };

View file

@ -2,7 +2,8 @@
let
tools = pkgs.callPackage ../tools.nix { };
in tools.runTest {
in
tools.runTest {
name = "colmena-exec";
colmena.test = {

View file

@ -1,6 +1,7 @@
let
tools = import ./tools.nix { insideVm = true; };
in {
in
{
meta = {
nixpkgs = tools.pkgs;
};

View file

@ -1,7 +1,8 @@
{ pkgs
, evaluator ? "chunked"
, extraApplyFlags ? ""
, pure ? true
{
pkgs,
evaluator ? "chunked",
extraApplyFlags ? "",
pure ? true,
}:
let
@ -11,24 +12,23 @@ let
targets = [ "alpha" ];
};
applyFlags = "--evaluator ${evaluator} ${extraApplyFlags}"
+ lib.optionalString (!pure) "--impure";
applyFlags = "--evaluator ${evaluator} ${extraApplyFlags}" + lib.optionalString (!pure) "--impure";
# From integration-tests/nixpkgs.nix
colmenaFlakeInputs = pkgs._inputs;
in tools.runTest {
name = "colmena-flakes-${evaluator}"
+ lib.optionalString (!pure) "-impure";
in
tools.runTest {
name = "colmena-flakes-${evaluator}" + lib.optionalString (!pure) "-impure";
nodes.deployer = {
virtualisation.additionalPaths =
lib.mapAttrsToList (k: v: v.outPath) colmenaFlakeInputs;
virtualisation.additionalPaths = lib.mapAttrsToList (k: v: v.outPath) colmenaFlakeInputs;
};
colmena.test = {
bundle = ./.;
testScript = ''
testScript =
''
import re
deployer.succeed("sed -i 's @nixpkgs@ path:${pkgs._inputs.nixpkgs.outPath}?narHash=${pkgs._inputs.nixpkgs.narHash} g' /tmp/bundle/flake.nix")
@ -54,7 +54,8 @@ in tools.runTest {
deployer.succeed("cd /tmp/bundle && ${tools.colmenaExec} apply --on @target ${applyFlags}")
alpha.succeed("grep SECOND /etc/deployment")
'' + lib.optionalString pure ''
''
+ lib.optionalString pure ''
with subtest("Check that impure expressions are forbidden"):
deployer.succeed("sed -i 's|SECOND|''${builtins.readFile /etc/hostname}|g' /tmp/bundle/probe.nix")
logs = deployer.fail("cd /tmp/bundle && run-copy-stderr ${tools.colmenaExec} apply --on @target ${applyFlags}")

View file

@ -6,11 +6,18 @@
colmena.url = "@colmena@";
};
outputs = { self, nixpkgs, colmena }: let
outputs =
{
self,
nixpkgs,
colmena,
}:
let
pkgs = import nixpkgs {
system = "x86_64-linux";
};
in {
in
{
colmena = import ./hive.nix { inherit pkgs; };
colmenaHive = colmena.lib.makeHive self.outputs.colmena;
};

View file

@ -6,7 +6,8 @@ let
insideVm = true;
targets = [ "alpha" ];
};
in {
in
{
meta = {
nixpkgs = tools.pkgs;
};

View file

@ -1,6 +1,7 @@
let
flake = (import ../flake-compat.nix).defaultNix;
in import flake.inputs.stable.outPath {
in
import flake.inputs.stable.outPath {
overlays = [
flake.overlay

View file

@ -1,6 +1,7 @@
let
flake = (import ../flake-compat.nix).defaultNix;
in import flake.inputs.nixpkgs.outPath {
in
import flake.inputs.nixpkgs.outPath {
overlays = [
flake.overlay

View file

@ -2,7 +2,8 @@
let
tools = pkgs.callPackage ../tools.nix { };
in tools.runTest {
in
tools.runTest {
name = "colmena-parallel";
colmena.test = {

View file

@ -1,6 +1,7 @@
let
tools = import ./tools.nix { insideVm = true; };
in {
in
{
meta = {
nixpkgs = tools.pkgs;
};

View file

@ -5,14 +5,19 @@
#
# TODO: Modularize most of this
{ insideVm ? false
, deployers ? [ "deployer" ] # Nodes configured as deployers (with Colmena and pre-built system closure)
, targets ? [ "alpha" "beta" "gamma" ] # Nodes configured as targets (minimal config)
, extraDeployerConfig ? {} # Extra config on the deployer
, prebuiltTarget ? "alpha" # Target node to prebuild system closure for, or null
{
insideVm ? false,
deployers ? [ "deployer" ], # Nodes configured as deployers (with Colmena and pre-built system closure)
targets ? [
"alpha"
"beta"
"gamma"
], # Nodes configured as targets (minimal config)
extraDeployerConfig ? { }, # Extra config on the deployer
prebuiltTarget ? "alpha", # Target node to prebuild system closure for, or null
, pkgs ? if insideVm then import <nixpkgs> {} else throw "Must specify pkgs"
, colmena ? if !insideVm then pkgs.colmena else throw "Cannot eval inside VM"
pkgs ? if insideVm then import <nixpkgs> { } else throw "Must specify pkgs",
colmena ? if !insideVm then pkgs.colmena else throw "Cannot eval inside VM",
}:
with builtins;
@ -28,9 +33,13 @@ let
sshKeys = import (pkgs.path + "/nixos/tests/ssh-keys.nix") pkgs;
nixosLib = import (pkgs.path + "/nixos/lib") { };
inputClosureOf = pkg: pkgs.runCommand "full-closure" {
inputClosureOf =
pkg:
pkgs.runCommand "full-closure"
{
refs = pkgs.writeReferencesToFile pkg.drvPath;
} ''
}
''
touch $out
while read ref; do
@ -43,7 +52,9 @@ let
'';
## The modular NixOS test framework with Colmena additions
colmenaTestModule = { lib, config, ... }: let
colmenaTestModule =
{ lib, config, ... }:
let
cfg = config.colmena.test;
targetList = "[${concatStringsSep ", " targets}]";
@ -57,7 +68,8 @@ let
cp ${./tools.nix} $out/tools.nix
'';
};
in {
in
{
options = {
colmena.test = {
bundle = lib.mkOption {
@ -79,11 +91,14 @@ let
};
};
config = {
testScript = ''
testScript =
''
start_all()
'' + lib.optionalString (prebuiltTarget != null) ''
''
+ lib.optionalString (prebuiltTarget != null) ''
deployer.succeed("nix-store -qR ${prebuiltSystem}")
'' + ''
''
+ ''
deployer.succeed("nix-store -qR ${pkgs.path}")
deployer.succeed("ln -sf ${pkgs.path} /nixpkgs")
deployer.succeed("mkdir -p /root/.ssh && touch /root/.ssh/id_rsa && chmod 600 /root/.ssh/id_rsa && cat ${sshKeys.snakeOilPrivateKey} > /root/.ssh/id_rsa")
@ -109,7 +124,9 @@ let
'';
};
};
evalTest = module: nixosLib.evalTest {
evalTest =
module:
nixosLib.evalTest {
imports = [
module
colmenaTestModule
@ -124,7 +141,14 @@ let
# We include the input closure of a prebuilt system profile
# so it can build system profiles for the targets without
# network access.
deployerConfig = { pkgs, lib, config, ... }: {
deployerConfig =
{
pkgs,
lib,
config,
...
}:
{
imports = [
extraDeployerConfig
];
@ -142,9 +166,11 @@ let
virtualisation = {
memorySize = 6144;
writableStore = true;
additionalPaths = [
additionalPaths =
[
"${pkgs.path}"
] ++ lib.optionals (prebuiltTarget != null) [
]
++ lib.optionals (prebuiltTarget != null) [
prebuiltSystem
(inputClosureOf prebuiltSystem)
];
@ -173,7 +199,9 @@ let
# Setup for target nodes
#
# Kept as minimal as possible.
targetConfig = { lib, ... }: {
targetConfig =
{ lib, ... }:
{
nix.settings.substituters = lib.mkForce [ ];
documentation.nixos.enable = lib.mkOverride 60 true;
@ -188,19 +216,27 @@ let
system.switch.enable = true;
};
nodes = let
nodes =
let
deployerNodes = map (name: lib.nameValuePair name deployerConfig) deployers;
targetNodes = map (name: lib.nameValuePair name targetConfig) targets;
in listToAttrs (deployerNodes ++ targetNodes);
in
listToAttrs (deployerNodes ++ targetNodes);
# A "shallow" re-evaluation of the test for use from Colmena
standaloneTest = evalTest ({ ... }: {
standaloneTest = evalTest (
{ ... }:
{
inherit nodes;
});
}
);
prebuiltSystem = standaloneTest.config.nodes.${prebuiltTarget}.system.build.toplevel;
getStandaloneConfigFor = node: { lib, config, ... }: {
getStandaloneConfigFor =
node:
{ lib, config, ... }:
{
imports = [
(pkgs.path + "/nixos/lib/testing/nixos-test-base.nix")
(if elem node deployers then deployerConfig else targetConfig)
@ -215,12 +251,27 @@ let
deployment.tags = lib.optional (config.networking.hostName != "deployer") "target";
};
in {
inherit pkgs nodes colmena colmenaExec
getStandaloneConfigFor inputClosureOf;
in
{
inherit
pkgs
nodes
colmena
colmenaExec
getStandaloneConfigFor
inputClosureOf
;
runTest = module: (evalTest ({ config, ... }: {
imports = [ module { inherit nodes; } ];
runTest =
module:
(evalTest (
{ config, ... }:
{
imports = [
module
{ inherit nodes; }
];
result = config.test;
})).config.result;
}
)).config.result;
}

View file

@ -1,9 +1,16 @@
{ lib, stdenv, runCommand, colmena, ansi2html }:
{
lib,
stdenv,
runCommand,
colmena,
ansi2html,
}:
with builtins;
let
subcommands = [
subcommands =
[
null
"apply"
]
@ -16,9 +23,12 @@ let
"nix-info"
"repl"
];
renderHelp = subcommand: let
renderHelp =
subcommand:
let
fullCommand = if subcommand == null then "colmena" else "colmena ${subcommand}";
in ''
in
''
(
echo '## `${fullCommand}`'
echo -n '<pre><div class="hljs">'
@ -26,8 +36,17 @@ let
echo '</div></pre>'
)>>$out
'';
in runCommand "colmena-colorized-help" {
nativeBuildInputs = [ colmena ansi2html ];
} (''
in
runCommand "colmena-colorized-help"
{
nativeBuildInputs = [
colmena
ansi2html
];
}
(
''
ansi2html -H > $out
'' + concatStringsSep "\n" (map renderHelp subcommands))
''
+ concatStringsSep "\n" (map renderHelp subcommands)
)

View file

@ -1,23 +1,33 @@
{ lib, stdenv, nix-gitignore, mdbook, mdbook-linkcheck, python3, callPackage, writeScript
, deploymentOptionsMd ? null
, metaOptionsMd ? null
, colmena ? null
{
lib,
stdenv,
nix-gitignore,
mdbook,
mdbook-linkcheck,
python3,
callPackage,
writeScript,
deploymentOptionsMd ? null,
metaOptionsMd ? null,
colmena ? null,
# Full version
, version ? if colmena != null then colmena.version else "unstable"
version ? if colmena != null then colmena.version else "unstable",
# Whether this build is unstable
, unstable ? version == "unstable" || lib.hasInfix "-" version
unstable ? version == "unstable" || lib.hasInfix "-" version,
}:
let
apiVersion = builtins.concatStringsSep "." (lib.take 2 (lib.splitString "." version));
colorizedHelp = let
colorizedHelp =
let
help = callPackage ./colorized-help.nix {
inherit colmena;
};
in if colmena != null then help else null;
in
if colmena != null then help else null;
redirectTemplate = lib.escapeShellArg ''
<!doctype html>
@ -33,16 +43,29 @@ let
</html>
'';
in stdenv.mkDerivation {
inherit version deploymentOptionsMd metaOptionsMd colorizedHelp;
in
stdenv.mkDerivation {
inherit
version
deploymentOptionsMd
metaOptionsMd
colorizedHelp
;
pname = "colmena-manual" + (if unstable then "-unstable" else "");
src = nix-gitignore.gitignoreSource [ ] ./.;
nativeBuildInputs = [ mdbook mdbook-linkcheck python3 ];
nativeBuildInputs = [
mdbook
mdbook-linkcheck
python3
];
outputs = [ "out" "redirectFarm" ];
outputs = [
"out"
"redirectFarm"
];
COLMENA_VERSION = version;
COLMENA_UNSTABLE = unstable;

View file

@ -1,9 +1,10 @@
{ lib
, stdenv
, rustPlatform
, nix-gitignore
, installShellFiles
, nix-eval-jobs
{
lib,
stdenv,
rustPlatform,
nix-gitignore,
installShellFiles,
nix-eval-jobs,
}:
rustPlatform.buildRustPackage rec {
@ -20,7 +21,7 @@ rustPlatform.buildRustPackage rec {
buildInputs = [ nix-eval-jobs ];
NIX_EVAL_JOBS = "${nix-eval-jobs}/bin/nix-eval-jobs";
env.NIX_EVAL_JOBS = lib.getExe nix-eval-jobs;
preBuild = ''
if [[ -z "$NIX_EVAL_JOBS" ]]; then

View file

@ -1,3 +1 @@
let
flake = import ./flake-compat.nix;
in flake.shellNix
(import ./flake-compat.nix).shellNix

View file

@ -1,10 +1,8 @@
{ rawHive ? null # Colmena Hive attrset
, rawFlake ? null # Nix Flake attrset with `outputs.colmena`
, hermetic ? rawFlake != null # Whether we are allowed to use <nixpkgs>
, colmenaOptions ? import ./options.nix
, colmenaModules ? import ./modules.nix
{
rawHive ? null, # Colmena Hive attrset
rawFlake ? null, # Nix Flake attrset with `outputs.colmena`
hermetic ? rawFlake != null, # Whether we are allowed to use <nixpkgs>
}:
with builtins;
let
defaultHive = {
@ -17,37 +15,60 @@ let
defaults = { };
};
uncheckedHive =
let
flakeToHive =
rawFlake:
if rawFlake.outputs ? colmena then
rawFlake.outputs.colmena
else
throw "Flake must define outputs.colmena.";
uncheckedHive = let
flakeToHive = rawFlake:
if rawFlake.outputs ? colmena then rawFlake.outputs.colmena else throw "Flake must define outputs.colmena.";
rawToHive = rawHive:
if typeOf rawHive == "lambda" || rawHive ? __functor then rawHive {}
else if typeOf rawHive == "set" then rawHive
else throw "The config must evaluate to an attribute set.";
rawToHive =
rawHive:
if (builtins.isFunction rawHive) || rawHive ? __functor then
rawHive { }
else if (builtins.isAttrs rawHive) then
rawHive
else
throw "The config must evaluate to an attribute set.";
in
if rawHive != null then rawToHive rawHive
else if rawFlake != null then flakeToHive rawFlake
else throw "Either a plain Hive attribute set or a Nix Flake attribute set must be specified.";
if rawHive != null then
rawToHive rawHive
else if rawFlake != null then
flakeToHive rawFlake
else
throw "Either a plain Hive attribute set or a Nix Flake attribute set must be specified.";
uncheckedUserMeta =
if uncheckedHive ? meta && uncheckedHive ? network then
throw "Only one of `network` and `meta` may be specified. `meta` should be used as `network` is for NixOps compatibility."
else if uncheckedHive ? meta then uncheckedHive.meta
else if uncheckedHive ? network then uncheckedHive.network
else {};
else if uncheckedHive ? meta then
uncheckedHive.meta
else if uncheckedHive ? network then
uncheckedHive.network
else
{ };
uncheckedRegistries = if uncheckedHive ? registry then uncheckedHive.registry else { };
# The final hive will always have the meta key instead of network.
hive = let
userMeta = (lib.modules.evalModules {
modules = [ colmenaOptions.metaOptions uncheckedUserMeta ];
hive =
let
userMeta =
(lib.modules.evalModules {
modules = [
./options/meta.nix
uncheckedUserMeta
];
}).config;
registry = (lib.modules.evalModules {
modules = [ colmenaOptions.registryOptions { registry = uncheckedRegistries; } ];
registry =
(lib.modules.evalModules {
modules = [
./options/registry.nix
{ registry = uncheckedRegistries; }
];
}).config.registry;
mergedHive =
@ -56,24 +77,32 @@ let
Hint: Use the `colmenaHive` output instead of `colmena`.
'';
removeAttrs (defaultHive // uncheckedHive) [ "meta" "network" "registry" ];
removeAttrs (defaultHive // uncheckedHive) [
"meta"
"network"
"registry"
];
meta = {
meta =
if !hermetic && userMeta.nixpkgs == null
then userMeta // { nixpkgs = <nixpkgs>; }
else userMeta;
if !hermetic && userMeta.nixpkgs == null then userMeta // { nixpkgs = <nixpkgs>; } else userMeta;
};
in mergedHive // meta // { inherit registry; };
in
mergedHive // meta // { inherit registry; };
configsFor = node: let
configsFor =
node:
let
nodeConfig = hive.${node};
in
assert lib.assertMsg (!elem node reservedNames) "\"${node}\" is a reserved name and cannot be used as the name of a node";
if typeOf nodeConfig == "list" then nodeConfig
else [ nodeConfig ];
assert lib.assertMsg (
!builtins.elem node reservedNames
) "\"${node}\" is a reserved name and cannot be used as the name of a node";
if (builtins.isList nodeConfig) then nodeConfig else [ nodeConfig ];
mkNixpkgs = configName: pkgConf: let
mkNixpkgs =
configName: pkgConf:
let
uninitializedError = typ: ''
Passing ${typ} as ${configName} is no longer accepted with Flakes.
Please initialize Nixpkgs like the following:
@ -91,17 +120,18 @@ let
}
'';
in
if typeOf pkgConf == "path" || (typeOf pkgConf == "set" && pkgConf ? outPath) then
if hermetic then throw (uninitializedError "a path to Nixpkgs")
if (builtins.isPath pkgConf) || ((builtins.isAttrs pkgConf) && pkgConf ? outPath) then
if hermetic then
throw (uninitializedError "a path to Nixpkgs")
# The referenced file might return an initialized Nixpkgs attribute set directly
else mkNixpkgs configName (import pkgConf)
else if typeOf pkgConf == "lambda" then
if hermetic then throw (uninitializedError "a Nixpkgs lambda")
else pkgConf { overlays = []; }
else if typeOf pkgConf == "set" then
if pkgConf ? outputs then throw (uninitializedError "an uninitialized Nixpkgs input")
else pkgConf
else throw ''
else
mkNixpkgs configName (import pkgConf)
else if (builtins.isFunction pkgConf) then
if hermetic then throw (uninitializedError "a Nixpkgs lambda") else pkgConf { overlays = [ ]; }
else if (builtins.isAttrs pkgConf) then
if pkgConf ? outputs then throw (uninitializedError "an uninitialized Nixpkgs input") else pkgConf
else
throw ''
${configName} must be one of:
- A path to Nixpkgs (e.g., <nixpkgs>)
@ -109,89 +139,131 @@ let
- A Nixpkgs attribute set
'';
nixpkgs = let
nixpkgs =
let
# Can't rely on the module system yet
nixpkgsConf =
if uncheckedUserMeta ? nixpkgs then uncheckedUserMeta.nixpkgs
else if hermetic then throw "meta.nixpkgs must be specified in hermetic mode."
else <nixpkgs>;
in mkNixpkgs "meta.nixpkgs" nixpkgsConf;
if uncheckedUserMeta ? nixpkgs then
uncheckedUserMeta.nixpkgs
else if hermetic then
throw "meta.nixpkgs must be specified in hermetic mode."
else
<nixpkgs>;
in
mkNixpkgs "meta.nixpkgs" nixpkgsConf;
lib = nixpkgs.lib;
reservedNames = [ "defaults" "network" "meta" "registry" ];
reservedNames = [
"defaults"
"network"
"meta"
"registry"
];
evalNode = name: configs:
evalNode =
name: configs:
# Some help on error messages.
assert (lib.assertMsg (lib.hasAttrByPath [ "deployment" "systemType" ] hive.${name})
"${name} does not have a deployment system type!");
assert (lib.assertMsg (builtins.typeOf hive.registry == "set"))
"The hive's registry is not a set, but of type '${builtins.typeOf hive.registry}'";
assert (lib.assertMsg (lib.hasAttr hive.${name}.deployment.systemType hive.registry)
"${builtins.toJSON (hive.${name}.deployment.systemType)} does not exist in the registry of systems!");
assert (
lib.assertMsg (lib.hasAttrByPath [
"deployment"
"systemType"
] hive.${name}) "${name} does not have a deployment system type!"
);
assert (lib.assertMsg (
builtins.isAttrs hive.registry
)) "The hive's registry is not a set, but of type '${builtins.typeOf hive.registry}'";
assert (
lib.assertMsg (lib.hasAttr hive.${name}.deployment.systemType hive.registry)
"${builtins.toJSON (hive.${name}.deployment.systemType)} does not exist in the registry of systems!"
);
let
# We cannot use `configs` because we need to access to the raw configuration fragment.
inherit (hive.registry.${hive.${name}.deployment.systemType}) evalConfig;
npkgs =
if hasAttr name hive.meta.nodeNixpkgs
then mkNixpkgs "meta.nodeNixpkgs.${name}" hive.meta.nodeNixpkgs.${name}
else nixpkgs;
if builtins.hasAttr name hive.meta.nodeNixpkgs then
mkNixpkgs "meta.nodeNixpkgs.${name}" hive.meta.nodeNixpkgs.${name}
else
nixpkgs;
# Here we need to merge the configurations in meta.nixpkgs
# and in machine config.
nixpkgsModule = { config, lib, ... }: let
nixpkgsModule =
{ config, lib, ... }:
let
hasTypedConfig = lib.versionAtLeast lib.version "22.11pre";
in {
in
{
nixpkgs.overlays = lib.mkBefore npkgs.overlays;
nixpkgs.config = if hasTypedConfig then lib.mkBefore npkgs.config else lib.mkOptionDefault npkgs.config;
nixpkgs.config =
if hasTypedConfig then lib.mkBefore npkgs.config else lib.mkOptionDefault npkgs.config;
warnings = let
warnings =
let
# Before 22.11, most config keys were untyped thus the merging
# was broken. Let's warn the user if not all config attributes
# set in meta.nixpkgs are overridden.
metaKeys = attrNames npkgs.config;
nodeKeys = [ "doCheckByDefault" "warnings" "allowAliases" ] ++ (attrNames config.nixpkgs.config);
remainingKeys = filter (k: ! elem k nodeKeys) metaKeys;
metaKeys = builtins.attrNames npkgs.config;
nodeKeys = [
"doCheckByDefault"
"warnings"
"allowAliases"
] ++ (builtins.attrNames config.nixpkgs.config);
remainingKeys = builtins.filter (k: !builtins.elem k nodeKeys) metaKeys;
in
lib.optional (!hasTypedConfig && length remainingKeys != 0)
lib.optional (!hasTypedConfig && builtins.length remainingKeys != 0)
"The following Nixpkgs configuration keys set in meta.nixpkgs will be ignored: ${toString remainingKeys}";
} // lib.optionalAttrs (builtins.hasAttr "localSystem" npkgs || builtins.hasAttr "crossSystem" npkgs) {
}
//
lib.optionalAttrs (builtins.hasAttr "localSystem" npkgs || builtins.hasAttr "crossSystem" npkgs)
{
nixpkgs.localSystem = lib.mkBefore npkgs.localSystem;
nixpkgs.crossSystem = lib.mkBefore npkgs.crossSystem;
};
in evalConfig {
in
evalConfig {
# This doesn't exist for `evalModules` the generic way.
# inherit (npkgs) system;
modules = [
nixpkgsModule
colmenaModules.assertionModule
colmenaOptions.deploymentOptions
./modules/assertions.nix
./options/deployment.nix
(hive.registry.${hive.${name}.deployment.systemType}.defaults or hive.defaults)
] ++ configs;
specialArgs = {
specialArgs =
{
inherit name;
nodes = uncheckedNodes;
} // hive.meta.specialArgs // (hive.meta.nodeSpecialArgs.${name} or {});
}
// hive.meta.specialArgs
// (hive.meta.nodeSpecialArgs.${name} or { });
};
nodeNames = filter (name: ! elem name reservedNames) (attrNames hive);
nodeNames = builtins.filter (name: !builtins.elem name reservedNames) (builtins.attrNames hive);
# Used as the `nodes` argument in modules. We skip recursive type checking
# for performance.
uncheckedNodes = listToAttrs (map (name: let
uncheckedNodes = builtins.listToAttrs (
builtins.map (
name:
let
configs = [
{
_module.check = false;
}
] ++ configsFor name;
in {
in
{
inherit name;
value = evalNode name configs;
}) nodeNames);
}
) nodeNames
);
# Add required config Key here since we don't want to eval nixpkgs
metaConfigKeys = [
"name" "description"
"name"
"description"
"machinesFile"
"allowApplyAll"
];
@ -200,20 +272,35 @@ let
"supportsDeployment"
];
in rec {
in
rec {
# Exported attributes
__schema = "v0.20241006";
nodes = listToAttrs (map (name: { inherit name; value = evalNode name (configsFor name); }) nodeNames);
nodes = builtins.listToAttrs (
builtins.map (name: {
inherit name;
value = evalNode name (configsFor name);
}) nodeNames
);
toplevel = lib.mapAttrs (_: v: v.config.system.build.toplevel) nodes;
deploymentConfig = lib.mapAttrs (_: v: v.config.deployment) nodes;
deploymentConfigSelected = names: lib.filterAttrs (name: _: elem name names) deploymentConfig;
evalSelected = names: lib.filterAttrs (name: _: elem name names) toplevel;
deploymentConfigSelected =
names: lib.filterAttrs (name: _: builtins.elem name names) deploymentConfig;
evalSelected = names: lib.filterAttrs (name: _: builtins.elem name names) toplevel;
evalSelectedDrvPaths = names: lib.mapAttrs (_: v: v.drvPath) (evalSelected names);
metaConfig = lib.filterAttrs (n: v: elem n metaConfigKeys) hive.meta;
metaConfig = lib.filterAttrs (n: v: builtins.elem n metaConfigKeys) hive.meta;
# We cannot perform a `metaConfigKeys`-style simple check here
# because registry is arbitrarily deep and may evaluate nixpkgs indirectly.
registryConfig = lib.mapAttrs (systemTypeName: systemType:
lib.filterAttrs (n: v: elem n serializableSystemTypeConfigKeys) systemType) hive.registry;
introspect = f: f { inherit lib; pkgs = nixpkgs; inherit nodes; };
registryConfig = lib.mapAttrs (
systemTypeName: systemType:
lib.filterAttrs (n: v: builtins.elem n serializableSystemTypeConfigKeys) systemType
) hive.registry;
introspect =
f:
f {
inherit lib;
pkgs = nixpkgs;
inherit nodes;
};
}

View file

@ -5,24 +5,28 @@
hive.url = "%hive%";
};
outputs = { self, hive }: {
processFlake = let
outputs =
{ hive, ... }:
{
processFlake =
let
compatibleSchema = "v0.20241006";
# Evaluates a raw hive.
#
# This uses the `colmena` output.
evalHive = rawFlake: import ./eval.nix {
evalHive =
rawFlake:
import ./eval.nix {
inherit rawFlake;
hermetic = true;
colmenaOptions = import ./options.nix;
colmenaModules = import ./modules.nix;
};
# Uses an already-evaluated hive.
#
# This uses the `colmenaHive` output.
checkPreparedHive = hiveOutput:
checkPreparedHive =
hiveOutput:
if !(hiveOutput ? __schema) then
throw ''
The colmenaHive output does not contain a valid evaluated hive.
@ -35,9 +39,9 @@
Hint: Use the same version of Colmena as in the Flake input.
''
else hiveOutput;
else
hiveOutput;
in
if hive.outputs ? colmenaHive then checkPreparedHive hive.outputs.colmenaHive
else evalHive hive;
if hive.outputs ? colmenaHive then checkPreparedHive hive.outputs.colmenaHive else evalHive hive;
};
}

View file

@ -1,109 +0,0 @@
with builtins; {
assertionModule = { config, lib, ... }: {
assertions = lib.mapAttrsToList (key: opts: let
nonNulls = l: filter (x: x != null) l;
in {
assertion = length (nonNulls [opts.text opts.keyCommand opts.keyFile]) == 1;
message =
let prefix = "${name}.deployment.keys.${key}";
in "Exactly one of `${prefix}.text`, `${prefix}.keyCommand` and `${prefix}.keyFile` must be set.";
}) config.deployment.keys;
};
# Change the ownership of all keys uploaded pre-activation
#
# This is built as part of the system profile.
# We must be careful not to access `text` / `keyCommand` / `keyFile` here
keyChownModule = { lib, config, ... }: let
preActivationKeys = lib.filterAttrs (name: key: key.uploadAt == "pre-activation") config.deployment.keys;
scriptDeps = if config.system.activationScripts ? groups then [ "groups" ] else [ "users" ];
commands = lib.mapAttrsToList (name: key: let
keyPath = "${key.destDir}/${name}";
in ''
if [ -f "${keyPath}" ]; then
if ! chown ${key.user}:${key.group} "${keyPath}"; then
# Error should be visible in stderr
failed=1
fi
else
>&2 echo "Key ${keyPath} does not exist. Skipping chown."
fi
'') preActivationKeys;
script = lib.stringAfter scriptDeps ''
# This script is injected by Colmena to change the ownerships
# of keys (`deployment.keys`) deployed before system activation.
>&2 echo "setting up key ownerships..."
# We set the ownership of as many keys as possible before failing
failed=
${concatStringsSep "\n" commands}
if [ -n "$failed" ]; then
>&2 echo "Failed to set the ownership of some keys."
# The activation script has a trap to handle failed
# commands and print out various debug information.
# Let's trigger that instead of `exit 1`.
false
fi
'';
in {
system.activationScripts.colmena-chown-keys = lib.mkIf (length commands != 0) script;
};
# Create "${name}-key" services for NixOps compatibility
#
# This is built as part of the system profile.
# We must be careful not to access `text` / `keyCommand` / `keyFile` here
#
# Sadly, path units don't automatically deactivate the bound units when
# the key files are deleted, so we use inotifywait in the services' scripts.
#
# <https://github.com/systemd/systemd/issues/3642>
keyServiceModule = { pkgs, lib, config, ... }: {
systemd.paths = lib.mapAttrs' (name: val: {
name = "${name}-key";
value = {
wantedBy = [ "paths.target" ];
pathConfig = {
PathExists = val.path;
};
};
}) config.deployment.keys;
systemd.services = lib.mapAttrs' (name: val: {
name = "${name}-key";
value = {
enable = true;
serviceConfig = {
TimeoutStartSec = "infinity";
Restart = "always";
RestartSec = "100ms";
};
path = [ pkgs.inotify-tools ];
preStart = ''
(while read f; do if [ "$f" = "${val.name}" ]; then break; fi; done \
< <(inotifywait -qm --format '%f' -e create,move ${val.destDir}) ) &
if [[ -e "${val.path}" ]]; then
echo 'flapped down'
kill %1
exit 0
fi
wait %1
'';
script = ''
inotifywait -qq -e delete_self "${val.path}" &
if [[ ! -e "${val.path}" ]]; then
echo 'flapped up'
exit 0
fi
wait %1
'';
};
}) config.deployment.keys;
};
}

View file

@ -0,0 +1,28 @@
{
config,
lib,
name,
...
}:
let
inherit (lib) filter mapAttrsToList;
in
{
assertions = mapAttrsToList (key: opts: {
assertion =
builtins.length (
filter (x: x != null) [
opts.text
opts.keyCommand
opts.keyFile
]
) == 1;
message =
let
prefix = "${name}.deployment.keys.${key}";
in
"Exactly one of `${prefix}.text`, `${prefix}.keyCommand` and `${prefix}.keyFile` must be set.";
}) config.deployment.keys;
}

View file

@ -0,0 +1,64 @@
# Change the ownership of all keys uploaded pre-activation
#
# This is built as part of the system profile.
# We must be careful not to access `text` / `keyCommand` / `keyFile` here
{ config, lib, ... }:
let
inherit (lib)
concatStringsSep
filterAttrs
mapAttrsToList
mkIf
stringAfter
;
preActivationKeys = filterAttrs (
_: { uploadAt, ... }: uploadAt == "pre-activation"
) config.deployment.keys;
scriptDeps = if config.system.activationScripts ? groups then [ "groups" ] else [ "users" ];
commands = mapAttrsToList (
name: key:
let
keyPath = "${key.destDir}/${name}";
in
''
if [ -f "${keyPath}" ]; then
if ! chown ${key.user}:${key.group} "${keyPath}"; then
# Error should be visible in stderr
failed=1
fi
else
>&2 echo "Key ${keyPath} does not exist. Skipping chown."
fi
''
) preActivationKeys;
script = stringAfter scriptDeps ''
# This script is injected by Colmena to change the ownerships
# of keys (`deployment.keys`) deployed before system activation.
>&2 echo "setting up key ownerships..."
# We set the ownership of as many keys as possible before failing
failed=
${concatStringsSep "\n" commands}
if [ -n "$failed" ]; then
>&2 echo "Failed to set the ownership of some keys."
# The activation script has a trap to handle failed
# commands and print out various debug information.
# Let's trigger that instead of `exit 1`.
false
fi
'';
in
{
system.activationScripts.colmena-chown-keys = mkIf (commands != [ ]) script;
}

View file

@ -0,0 +1,61 @@
# Create "${name}-key" services for NixOps compatibility
#
# This is built as part of the system profile.
# We must be careful not to access `text` / `keyCommand` / `keyFile` here
#
# Sadly, path units don't automatically deactivate the bound units when
# the key files are deleted, so we use inotifywait in the services' scripts.
#
# <https://github.com/systemd/systemd/issues/3642>
{
config,
lib,
pkgs,
...
}:
let
inherit (lib) mapAttrs' nameValuePair;
in
{
systemd.paths = mapAttrs' (
name: value:
nameValuePair "${name}-key" {
wantedBy = [ "paths.target" ];
pathConfig.PathExists = value.path;
}
) config.deployment.keys;
systemd.services = mapAttrs' (
name: value:
nameValuePair "${name}-key" {
enable = true;
serviceConfig = {
TimeoutStartSec = "infinity";
Restart = "always";
RestartSec = "100ms";
};
path = [ pkgs.inotify-tools ];
preStart = ''
(while read f; do if [ "$f" = "${value.name}" ]; then break; fi; done \
< <(inotifywait -qm --format '%f' -e create,move ${value.destDir}) ) &
if [[ -e "${value.path}" ]]; then
echo 'flapped down'
kill %1
exit 0
fi
wait %1
'';
script = ''
inotifywait -qq -e delete_self "${value.path}" &
if [[ ! -e "${value.path}" ]]; then
echo 'flapped up'
exit 0
fi
wait %1
'';
}
) config.deployment.keys;
}

View file

@ -1,368 +0,0 @@
with builtins; rec {
keyType = { lib, name, config, ... }: let
inherit (lib) types;
in {
options = {
name = lib.mkOption {
description = ''
File name of the key.
'';
default = name;
type = types.str;
};
text = lib.mkOption {
description = ''
Content of the key.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
type = types.nullOr types.str;
};
keyFile = lib.mkOption {
description = ''
Path of the local file to read the key from.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
apply = value: if value == null then null else toString value;
type = types.nullOr types.path;
};
keyCommand = lib.mkOption {
description = ''
Command to run to generate the key.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
type = let
nonEmptyList = types.addCheck (types.listOf types.str) (l: length l > 0);
in types.nullOr nonEmptyList;
};
destDir = lib.mkOption {
description = ''
Destination directory on the host.
'';
default = "/run/keys";
type = types.path;
};
path = lib.mkOption {
description = ''
Full path to the destination.
'';
default = "${config.destDir}/${config.name}";
type = types.path;
internal = true;
};
user = lib.mkOption {
description = ''
The group that will own the file.
'';
default = "root";
type = types.str;
};
group = lib.mkOption {
description = ''
The group that will own the file.
'';
default = "root";
type = types.str;
};
permissions = lib.mkOption {
description = ''
Permissions to set for the file.
'';
default = "0600";
type = types.str;
};
uploadAt = lib.mkOption {
description = ''
When to upload the keys.
- pre-activation (default): Upload the keys before activating the new system profile.
- post-activation: Upload the keys after successfully activating the new system profile.
For `colmena upload-keys`, all keys are uploaded at the same time regardless of the configuration here.
'';
default = "pre-activation";
type = types.enum [ "pre-activation" "post-activation" ];
};
};
};
# Colmena-specific options
#
# Largely compatible with NixOps/Morph.
deploymentOptions = { name, lib, ... }: let
inherit (lib) types;
in {
options = {
deployment = {
systemType = lib.mkOption {
description = mdDoc ''
System type used for this node, e.g. NixOS.
'';
default = "nixos";
# TODO: enum among all registered systems?
type = types.str;
};
targetHost = lib.mkOption {
description = ''
The target SSH node for deployment.
By default, the node's attribute name will be used.
If set to null, only local deployment will be supported.
'';
type = types.nullOr types.str;
default = name;
};
targetPort = lib.mkOption {
description = ''
The target SSH port for deployment.
By default, the port is the standard port (22) or taken
from your ssh_config.
'';
type = types.nullOr types.ints.unsigned;
default = null;
};
targetUser = lib.mkOption {
description = ''
The user to use to log into the remote node. If set to null, the
target user will not be specified in SSH invocations.
'';
type = types.nullOr types.str;
default = "root";
};
allowLocalDeployment = lib.mkOption {
description = ''
Allow the configuration to be applied locally on the host running
Colmena.
For local deployment to work, all of the following must be true:
- The node must be running NixOS.
- The node must have deployment.allowLocalDeployment set to true.
- The node's networking.hostName must match the hostname.
To apply the configurations locally, run `colmena apply-local`.
You can also set deployment.targetHost to null if the nost is not
accessible over SSH (only local deployment will be possible).
'';
type = types.bool;
default = false;
};
buildOnTarget = lib.mkOption {
description = ''
Whether to build the system profiles on the target node itself.
When enabled, Colmena will copy the derivation to the target
node and initiate the build there. This avoids copying back the
build results involved with the native distributed build
feature. Furthermore, the `build` goal will be equivalent to
the `push` goal. Since builds happen on the target node, the
results are automatically "pushed" and won't exist in the local
Nix store.
You can temporarily override per-node settings by passing
`--build-on-target` (enable for all nodes) or
`--no-build-on-target` (disable for all nodes) on the command
line.
'';
type = types.bool;
default = false;
};
tags = lib.mkOption {
description = ''
A list of tags for the node.
Can be used to select a group of nodes for deployment.
'';
type = types.listOf types.str;
default = [];
};
keys = lib.mkOption {
description = ''
A set of secrets to be deployed to the node.
Secrets are transferred to the node out-of-band and
never ends up in the Nix store.
'';
type = types.attrsOf (types.submodule keyType);
default = {};
};
replaceUnknownProfiles = lib.mkOption {
description = ''
Allow a configuration to be applied to a host running a profile we
have no knowledge of. By setting this option to false, you reduce
the likelyhood of rolling back changes made via another Colmena user.
Unknown profiles are usually the result of either:
- The node had a profile applied, locally or by another Colmena.
- The host running Colmena garbage-collecting the profile.
To force profile replacement on all targeted nodes during apply,
use the flag `--force-replace-unknown-profiles`.
'';
type = types.bool;
default = true;
};
privilegeEscalationCommand = lib.mkOption {
description = ''
Command to use to elevate privileges when activating the new profiles on SSH hosts.
This is used on SSH hosts when `deployment.targetUser` is not `root`.
The user must be allowed to use the command non-interactively.
'';
type = types.listOf types.str;
default = [ "sudo" "-H" "--" ];
};
sshOptions = lib.mkOption {
description = ''
Extra SSH options to pass to the SSH command.
'';
type = types.listOf types.str;
default = [];
};
};
};
};
# Options for a registered system type
systemTypeOptions = { name, lib, ... }: let
inherit (lib) types;
mdDoc = lib.mdDoc or lib.id;
in
{
options = {
evalConfig = lib.mkOption {
description = mdDoc ''
Evaluation function which share the same interface as `nixos/lib/eval-config.nix`
which can be tailored to your own usecases or to target another type of system,
e.g. nix-darwin.
'';
type = types.functionTo types.unspecified;
};
supportsDeployment = lib.mkOption {
description = mdDoc ''
Whether this system type supports deployment or not.
If it supports deployment, it needs to have appropriate activation code,
refer to how to write custom activators.
'';
default = name == "nixos";
defaultText = "If a NixOS system, then true, otherwise false by default";
};
defaults = lib.mkOption {
description = mdDoc ''
Default configuration for that system type.
'';
type = types.functionTo types.unspecified;
default = _: {};
};
};
};
registryOptions = { lib, ... }: let
inherit (lib) types;
mdDoc = lib.mdDoc or lib.id;
in
{
options.registry = lib.mkOption {
description = mdDoc ''
A registry of all system types.
'';
type = types.attrsOf (types.submodule systemTypeOptions);
};
};
# Hive-wide options
metaOptions = { lib, ... }: let
inherit (lib) types;
in {
options = {
name = lib.mkOption {
description = ''
The name of the configuration.
'';
type = types.str;
default = "hive";
};
description = lib.mkOption {
description = ''
A short description for the configuration.
'';
type = types.str;
default = "A Colmena Hive";
};
nixpkgs = lib.mkOption {
description = ''
The pinned Nixpkgs package set. Accepts one of the following:
- A path to a Nixpkgs checkout
- The Nixpkgs lambda (e.g., import <nixpkgs>)
- An initialized Nixpkgs attribute set
This option must be specified when using Flakes.
'';
type = types.unspecified;
default = null;
};
nodeNixpkgs = lib.mkOption {
description = ''
Node-specific Nixpkgs pins.
'';
type = types.attrsOf types.unspecified;
default = {};
};
nodeSpecialArgs = lib.mkOption {
description = ''
Node-specific special args.
'';
type = types.attrsOf types.unspecified;
default = {};
};
machinesFile = lib.mkOption {
description = ''
Use the machines listed in this file when building this hive configuration.
If your Colmena host has nix configured to allow for remote builds
(for nix-daemon, your user being included in trusted-users)
you can set a machines file that will be passed to the underlying
nix-store command during derivation realization as a builders option.
For example, if you support multiple orginizations each with their own
build machine(s) you can ensure that builds only take place on your
local machine and/or the machines specified in this file.
See https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds
for the machine specification format.
This option is ignored when builds are initiated on the remote nodes
themselves via `deployment.buildOnTarget` or `--build-on-target`. To
still use the Nix distributed build functionality, configure the
builders on the target nodes with `nix.buildMachines`.
'';
default = null;
apply = value: if value == null then null else toString value;
type = types.nullOr types.path;
};
specialArgs = lib.mkOption {
description = ''
A set of special arguments to be passed to NixOS modules.
This will be merged into the `specialArgs` used to evaluate
the NixOS configurations.
'';
default = {};
type = types.attrsOf types.unspecified;
};
allowApplyAll = lib.mkOption {
description = ''
Whether to allow deployments without a node filter set.
If set to false, a node filter must be specified with `--on` when
deploying.
It helps prevent accidental deployments to the entire cluster
when tags are used (e.g., `@production` and `@staging`).
'';
default = true;
type = types.bool;
};
};
};
}

View file

@ -0,0 +1,151 @@
{ lib, name, ... }:
let
inherit (lib) mkOption;
inherit (lib.types)
attrsOf
bool
ints
listOf
nullOr
str
submodule
;
in
{
options = {
deployment = {
systemType = mkOption {
description = ''
System type used for this node, e.g. NixOS.
'';
default = "nixos";
# TODO: enum among all registered systems?
type = str;
};
targetHost = mkOption {
description = ''
The target SSH node for deployment.
By default, the node's attribute name will be used.
If set to null, only local deployment will be supported.
'';
type = nullOr str;
default = name;
};
targetPort = mkOption {
description = ''
The target SSH port for deployment.
By default, the port is the standard port (22) or taken
from your ssh_config.
'';
type = nullOr ints.unsigned;
default = null;
};
targetUser = mkOption {
description = ''
The user to use to log into the remote node. If set to null, the
target user will not be specified in SSH invocations.
'';
type = nullOr str;
default = "root";
};
allowLocalDeployment = mkOption {
description = ''
Allow the configuration to be applied locally on the host running
Colmena.
For local deployment to work, all of the following must be true:
- The node must be running NixOS.
- The node must have deployment.allowLocalDeployment set to true.
- The node's networking.hostName must match the hostname.
To apply the configurations locally, run `colmena apply-local`.
You can also set deployment.targetHost to null if the nost is not
accessible over SSH (only local deployment will be possible).
'';
type = bool;
default = false;
};
buildOnTarget = mkOption {
description = ''
Whether to build the system profiles on the target node itself.
When enabled, Colmena will copy the derivation to the target
node and initiate the build there. This avoids copying back the
build results involved with the native distributed build
feature. Furthermore, the `build` goal will be equivalent to
the `push` goal. Since builds happen on the target node, the
results are automatically "pushed" and won't exist in the local
Nix store.
You can temporarily override per-node settings by passing
`--build-on-target` (enable for all nodes) or
`--no-build-on-target` (disable for all nodes) on the command
line.
'';
type = bool;
default = false;
};
tags = mkOption {
description = ''
A list of tags for the node.
Can be used to select a group of nodes for deployment.
'';
type = listOf str;
default = [ ];
};
keys = mkOption {
description = ''
A set of secrets to be deployed to the node.
Secrets are transferred to the node out-of-band and
never ends up in the Nix store.
'';
type = attrsOf (submodule (import ./key.nix));
default = { };
};
replaceUnknownProfiles = mkOption {
description = ''
Allow a configuration to be applied to a host running a profile we
have no knowledge of. By setting this option to false, you reduce
the likelyhood of rolling back changes made via another Colmena user.
Unknown profiles are usually the result of either:
- The node had a profile applied, locally or by another Colmena.
- The host running Colmena garbage-collecting the profile.
To force profile replacement on all targeted nodes during apply,
use the flag `--force-replace-unknown-profiles`.
'';
type = bool;
default = true;
};
privilegeEscalationCommand = mkOption {
description = ''
Command to use to elevate privileges when activating the new profiles on SSH hosts.
This is used on SSH hosts when `deployment.targetUser` is not `root`.
The user must be allowed to use the command non-interactively.
'';
type = listOf str;
default = [
"sudo"
"-H"
"--"
];
};
sshOptions = mkOption {
description = ''
Extra SSH options to pass to the SSH command.
'';
type = listOf str;
default = [ ];
};
};
};
}

View file

@ -0,0 +1,120 @@
{
config,
lib,
name,
...
}:
let
inherit (lib) mkOption;
inherit (lib.types)
addCheck
enum
listOf
nullOr
path
str
;
in
{
options = {
name = mkOption {
description = ''
File name of the key.
'';
default = name;
type = str;
};
text = mkOption {
description = ''
Content of the key.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
type = nullOr str;
};
keyFile = mkOption {
description = ''
Path of the local file to read the key from.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
apply = value: if value == null then null else builtins.toString value;
type = nullOr path;
};
keyCommand = mkOption {
description = ''
Command to run to generate the key.
One of `text`, `keyCommand` and `keyFile` must be set.
'';
default = null;
type =
let
nonEmptyList = addCheck (listOf str) (l: builtins.length l > 0);
in
nullOr nonEmptyList;
};
destDir = mkOption {
description = ''
Destination directory on the host.
'';
default = "/run/keys";
type = path;
};
path = mkOption {
description = ''
Full path to the destination.
'';
default = "${config.destDir}/${config.name}";
type = path;
internal = true;
};
user = mkOption {
description = ''
The group that will own the file.
'';
default = "root";
type = str;
};
group = mkOption {
description = ''
The group that will own the file.
'';
default = "root";
type = str;
};
permissions = mkOption {
description = ''
Permissions to set for the file.
'';
default = "0600";
type = str;
};
uploadAt = mkOption {
description = ''
When to upload the keys.
- pre-activation (default): Upload the keys before activating the new system profile.
- post-activation: Upload the keys after successfully activating the new system profile.
For `colmena upload-keys`, all keys are uploaded at the same time regardless of the configuration here.
'';
default = "pre-activation";
type = enum [
"pre-activation"
"post-activation"
];
};
};
}

View file

@ -0,0 +1,114 @@
{ lib, ... }:
let
inherit (lib) mkOption;
inherit (lib.types)
attrsOf
bool
nullOr
path
str
unspecified
;
in
{
options = {
name = mkOption {
description = ''
The name of the configuration.
'';
type = str;
default = "hive";
};
description = mkOption {
description = ''
A short description for the configuration.
'';
type = str;
default = "A Colmena Hive";
};
nixpkgs = mkOption {
description = ''
The pinned Nixpkgs package set. Accepts one of the following:
- A path to a Nixpkgs checkout
- The Nixpkgs lambda (e.g., import <nixpkgs>)
- An initialized Nixpkgs attribute set
This option must be specified when using Flakes.
'';
type = unspecified;
default = null;
};
nodeNixpkgs = mkOption {
description = ''
Node-specific Nixpkgs pins.
'';
type = attrsOf unspecified;
default = { };
};
nodeSpecialArgs = mkOption {
description = ''
Node-specific special args.
'';
type = attrsOf unspecified;
default = { };
};
machinesFile = mkOption {
description = ''
Use the machines listed in this file when building this hive configuration.
If your Colmena host has nix configured to allow for remote builds
(for nix-daemon, your user being included in trusted-users)
you can set a machines file that will be passed to the underlying
nix-store command during derivation realization as a builders option.
For example, if you support multiple orginizations each with their own
build machine(s) you can ensure that builds only take place on your
local machine and/or the machines specified in this file.
See https://nixos.org/manual/nix/stable/advanced-topics/distributed-builds
for the machine specification format.
This option is ignored when builds are initiated on the remote nodes
themselves via `deployment.buildOnTarget` or `--build-on-target`. To
still use the Nix distributed build functionality, configure the
builders on the target nodes with `nix.buildMachines`.
'';
default = null;
apply = value: if value == null then null else builtins.toString value;
type = nullOr path;
};
specialArgs = mkOption {
description = ''
A set of special arguments to be passed to NixOS modules.
This will be merged into the `specialArgs` used to evaluate
the NixOS configurations.
'';
default = { };
type = attrsOf unspecified;
};
allowApplyAll = mkOption {
description = ''
Whether to allow deployments without a node filter set.
If set to false, a node filter must be specified with `--on` when
deploying.
It helps prevent accidental deployments to the entire cluster
when tags are used (e.g., `@production` and `@staging`).
'';
default = true;
type = bool;
};
};
}

View file

@ -0,0 +1,54 @@
{ lib, ... }:
let
inherit (lib) mkOption;
inherit (lib.types)
attrsOf
functionTo
submodule
unspecified
;
in
{
options.registry = mkOption {
description = ''
A registry of all system types.
'';
type = attrsOf (
submodule (
{ name, ... }:
{
options = {
evalConfig = mkOption {
description = ''
Evaluation function which share the same interface as `nixos/lib/eval-config.nix`
which can be tailored to your own usecases or to target another type of system,
e.g. nix-darwin.
'';
type = functionTo unspecified;
};
supportsDeployment = mkOption {
description = ''
Whether this system type supports deployment or not.
If it supports deployment, it needs to have appropriate activation code,
refer to how to write custom activators.
'';
default = name == "nixos";
defaultText = "If a NixOS system, then true, otherwise false by default";
};
defaults = mkOption {
description = ''
Default configuration for that system type.
'';
type = functionTo unspecified;
default = _: { };
};
};
}
)
);
};
}

View file

@ -3,7 +3,9 @@
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
colmena.url = "git+file://@repoPath@";
};
outputs = { nixpkgs, colmena, ... }: {
outputs =
{ nixpkgs, colmena, ... }:
{
colmenaHive = colmena.lib.makeHive {
meta = {
nixpkgs = import nixpkgs {
@ -11,7 +13,14 @@
};
};
host-a = { name, nodes, pkgs, ... }: {
host-a =
{
name,
nodes,
pkgs,
...
}:
{
boot.isContainer = true;
time.timeZone = nodes.host-b.config.time.timeZone;
};

View file

@ -2,7 +2,9 @@
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
};
outputs = { nixpkgs, ... }: {
outputs =
{ nixpkgs, ... }:
{
colmena = {
meta = {
nixpkgs = import nixpkgs {
@ -10,7 +12,14 @@
};
};
host-a = { name, nodes, pkgs, ... }: {
host-a =
{
name,
nodes,
pkgs,
...
}:
{
boot.isContainer = true;
time.timeZone = nodes.host-b.config.time.timeZone;
};