aboutsummaryrefslogtreecommitdiffstats
path: root/nix/module.nix
diff options
context:
space:
mode:
Diffstat (limited to 'nix/module.nix')
-rw-r--r--nix/module.nix340
1 files changed, 340 insertions, 0 deletions
diff --git a/nix/module.nix b/nix/module.nix
new file mode 100644
index 0000000..f81bea9
--- /dev/null
+++ b/nix/module.nix
@@ -0,0 +1,340 @@
+# enstate on NixOS (docs/design.md, 5.3): the schema from options, the receptor run at
+# boot and after each switch, and the check before a switch.
+#
+# enstate.enable = true;
+# enstate.channels.secrets.slots."retiolum.key" = {
+# type = "application/x-openssh-private-key; algorithm=ed25519";
+# secret = true;
+# reactions.restart = [ "tincr-retiolum.service" ]; # unless it restarts itself
+# };
+# enstate.channels.fleet.slots.hosts.type = "application/x-enstate-hosts+json";
+# enstate.views.ssh-known-hosts = {
+# from = [ "fleet/hosts" ];
+# renderer = ./render-known-hosts;
+# };
+#
+# systemd.services.tincr-retiolum.serviceConfig.LoadCredential =
+# "retiolum.key:${config.enstate.channels.secrets.slots."retiolum.key".path}";
+#
+# Every slot is its own view, `CHANNEL.SLOT`, unless it says `view = false`; its file
+# is the slot's `path`, `/run/enstate/views/CHANNEL.SLOT/SLOT`. Services read views
+# as they like; one whose `LoadCredential=` reads a view is ordered after the unit that
+# puts the views in place, by itself.
+{
+ config,
+ lib,
+ pkgs,
+ ...
+}:
+
+let
+ inherit (lib) mkOption types;
+ cfg = config.enstate;
+
+ reactions = {
+ restart = mkOption {
+ type = types.listOf types.str;
+ default = [ ];
+ description = "Units to restart when the view changes (`foo.service`).";
+ };
+ reload = mkOption {
+ type = types.listOf types.str;
+ default = [ ];
+ description = "Units to reload (or restart, if they can't reload) when the view changes.";
+ };
+ };
+
+ slot =
+ channel:
+ types.submodule (
+ { name, config, ... }:
+ {
+ options = {
+ type = mkOption {
+ type = types.str;
+ description = "What the slot holds, as a media type.";
+ example = "application/x-openssh-private-key; algorithm=ed25519";
+ };
+ required = mkOption {
+ type = types.bool;
+ default = true;
+ description = "Whether a submission, or a switch, that leaves the slot unfilled is refused.";
+ };
+ secret = mkOption {
+ type = types.bool;
+ default = false;
+ description = "Whether the slot's views are readable by root only.";
+ };
+ renamedFrom = mkOption {
+ type = types.nullOr types.str;
+ default = null;
+ description = "The slot's name in an earlier generation, so its entry carries over, both ways.";
+ };
+ view = mkOption {
+ type = types.bool;
+ default = true;
+ description = "Whether the slot is its own view, `CHANNEL.SLOT`.";
+ };
+ path = mkOption {
+ type = types.nullOr types.str;
+ readOnly = true;
+ default = if config.view then "${cfg.runDir}/views/${channel}.${name}/${name}" else null;
+ defaultText = lib.literalExpression ''"''${config.enstate.runDir}/views/CHANNEL.SLOT/SLOT"'';
+ description = "Where the slot's own view puts its entry, if it has one, for services to read.";
+ };
+ inherit reactions;
+ };
+ }
+ );
+
+ # The views of slots that are their own.
+ slotViews = lib.concatMapAttrs (
+ channel: channelCfg:
+ lib.concatMapAttrs (
+ name: slotCfg:
+ lib.optionalAttrs slotCfg.view {
+ "${channel}.${name}" = {
+ from = [ "${channel}/${name}" ];
+ inherit (slotCfg) reactions;
+ };
+ }
+ ) channelCfg.slots
+ ) cfg.channels;
+
+ nonNull = lib.filterAttrs (_: value: value != null);
+
+ schema = {
+ schema = 1;
+ types = lib.mapAttrs (
+ _: typeCfg: nonNull { validator = lib.mapNullable toString typeCfg.validator; }
+ ) cfg.types;
+ channels = lib.mapAttrs (_: channelCfg: {
+ slots = lib.mapAttrs (
+ _: slotCfg:
+ nonNull {
+ inherit (slotCfg)
+ type
+ required
+ secret
+ renamedFrom
+ ;
+ }
+ ) channelCfg.slots;
+ }) cfg.channels;
+ validators = lib.mapAttrs (_: validatorCfg: {
+ inherit (validatorCfg) slots;
+ program = toString validatorCfg.program;
+ }) cfg.validators;
+ views =
+ slotViews
+ // lib.mapAttrs (
+ _: viewCfg:
+ nonNull {
+ inherit (viewCfg) from secret;
+ renderer = lib.mapNullable toString viewCfg.renderer;
+ reactions = { inherit (viewCfg.reactions) restart reload; };
+ }
+ ) cfg.views;
+ };
+
+ schemaFile = pkgs.writers.writeJSON "enstate-schema.json" schema;
+
+ settings = pkgs.writers.writeJSON "enstate-config.json" {
+ state-dir = cfg.stateDir;
+ run-dir = cfg.runDir;
+ };
+
+ enstate = lib.getExe cfg.package;
+
+
+ # Whether a `LoadCredential=` (`ID:PATH`) reads a view.
+ readsView =
+ credential:
+ let
+ parts = lib.splitString ":" credential;
+ in
+ lib.length parts > 1
+ && lib.hasPrefix "${cfg.runDir}/views/" (lib.concatStringsSep ":" (lib.tail parts));
+in
+{
+ options.enstate = {
+ enable = lib.mkEnableOption "enstate, state next to the Nix store";
+
+ package = mkOption {
+ type = types.package;
+ default = pkgs.callPackage ./package.nix { };
+ defaultText = lib.literalExpression "pkgs.callPackage ./package.nix { }";
+ description = "enstate, the program.";
+ };
+
+ stateDir = mkOption {
+ type = types.str;
+ default = "/var/lib/enstate";
+ description = "Where the snapshots are kept.";
+ };
+
+ runDir = mkOption {
+ type = types.str;
+ default = "/run/enstate";
+ description = "Where the views are put.";
+ };
+
+ types = mkOption {
+ type = types.attrsOf (
+ types.submodule {
+ options.validator = mkOption {
+ type = types.nullOr types.path;
+ default = null;
+ description = "The type's validator (docs/design.md, 5.2).";
+ };
+ }
+ );
+ default = { };
+ description = "Types, by media type, and their validators.";
+ };
+
+ channels = mkOption {
+ type = types.attrsOf (
+ types.submodule (
+ { name, ... }:
+ {
+ options.slots = mkOption {
+ type = types.attrsOf (slot name);
+ default = { };
+ description = "The channel's slots.";
+ };
+ }
+ )
+ );
+ default = { };
+ description = "Channels, and the slots this generation offers in them.";
+ };
+
+ validators = mkOption {
+ type = types.attrsOf (
+ types.submodule {
+ options = {
+ slots = mkOption {
+ type = types.listOf types.str;
+ description = "The slots it is given (`CHANNEL/SLOT`).";
+ };
+ program = mkOption {
+ type = types.path;
+ description = "The validator (docs/design.md, 5.2).";
+ };
+ };
+ }
+ );
+ default = { };
+ description = "Validators across slots.";
+ };
+
+ views = mkOption {
+ type = types.attrsOf (
+ types.submodule (
+ { name, ... }:
+ {
+ options = {
+ from = mkOption {
+ type = types.listOf types.str;
+ description = "The slots it is made from (`CHANNEL/SLOT`).";
+ };
+ renderer = mkOption {
+ type = types.nullOr types.path;
+ default = null;
+ description = "The renderer (docs/design.md, 5.2); none for a view of one entry.";
+ };
+ secret = mkOption {
+ type = types.nullOr types.bool;
+ default = null;
+ description = "Whether it is readable by root only; by default, if a slot it is made from is secret.";
+ };
+ path = mkOption {
+ type = types.str;
+ readOnly = true;
+ default = "${cfg.runDir}/views/${name}";
+ defaultText = lib.literalExpression ''"''${config.enstate.runDir}/views/NAME"'';
+ description = "The directory the view's files are in, for services to read.";
+ };
+ inherit reactions;
+ };
+ }
+ )
+ );
+ default = { };
+ description = "Views made by renderers.";
+ };
+
+ checkBeforeSwitch = mkOption {
+ type = types.bool;
+ default = true;
+ description = ''
+ Whether a switch to a generation whose required slots the state can't fill,
+ even by migrating, is refused before switching. Not checked when the running
+ system has no enstate, nor for `boot` and `dry-activate`.
+ '';
+ };
+ };
+
+ # A service whose `LoadCredential=` reads a view is ordered after the unit that puts
+ # the views in place, and wants it: without saying so itself.
+ options.systemd.services = mkOption {
+ type = types.attrsOf (
+ types.submodule (
+ { config, ... }:
+ let
+ credentials = lib.concatMap (option: lib.toList (config.serviceConfig.${option} or [ ])) [
+ "LoadCredential"
+ "LoadCredentialEncrypted"
+ ];
+ in
+ {
+ config = lib.mkIf (cfg.enable && lib.any readsView credentials) {
+ after = [ "enstate.service" ];
+ wants = [ "enstate.service" ];
+ };
+ }
+ )
+ );
+ };
+
+ config = lib.mkIf cfg.enable {
+ assertions = [
+ {
+ assertion = lib.intersectLists (lib.attrNames slotViews) (lib.attrNames cfg.views) == [ ];
+ message = "enstate.views: ${toString (lib.intersectLists (lib.attrNames slotViews) (lib.attrNames cfg.views))} is also a slot's own view";
+ }
+ ];
+
+ environment.systemPackages = [ cfg.package ];
+ environment.etc."enstate/schema.json".source = schemaFile;
+ environment.etc."enstate/config.json".source = settings;
+
+ # At boot and after a switch that changes the schema: the views of this
+ # generation, from the snapshots, and the reactions of those that changed.
+ systemd.services.enstate = {
+ description = "enstate: the views of this generation";
+ wantedBy = [ "multi-user.target" ];
+ after = [ "local-fs.target" ];
+ restartTriggers = [ schemaFile ];
+ serviceConfig = {
+ Type = "oneshot";
+ RemainAfterExit = true;
+ ExecStart = "${enstate} --migrate";
+ };
+ };
+
+ system.preSwitchChecks = lib.mkIf cfg.checkBeforeSwitch {
+ enstate = ''
+ case "''${2-}" in
+ boot|dry-activate) exit 0 ;;
+ esac
+ if [ ! -e /etc/enstate/schema.json ]; then
+ echo "enstate: the running system has none, not checking"
+ exit 0
+ fi
+ ${enstate} --migrate --dry-run --schema "$1/etc/enstate/schema.json"
+ '';
+ };
+ };
+}