From 5e43ee7918eb3cfae503221f073a8493bcf357f0 Mon Sep 17 00:00:00 2001 From: Steffen Vogel Date: Wed, 28 Feb 2024 00:13:53 +0100 Subject: [PATCH] nix: Add NixOS module for villas-node Signed-off-by: Steffen Vogel --- flake.nix | 12 +++++++++ packaging/nix/module.nix | 57 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 packaging/nix/module.nix diff --git a/flake.nix b/flake.nix index 699f32713..b26e86e76 100644 --- a/flake.nix +++ b/flake.nix @@ -208,5 +208,17 @@ # Standard flake attribute specifying the formatter invoked on `nix fmt` formatter = forSupportedSystems (system: (pkgsFor system).alejandra); + + # Standard flake attribute for NixOS modules + nixosModules = rec { + default = villas; + + villas = { + imports = [(nixDir + "/module.nix")]; + nixpkgs.overlays = [ + self.overlays.default + ]; + }; + }; }; } diff --git a/packaging/nix/module.nix b/packaging/nix/module.nix new file mode 100644 index 000000000..a0d295020 --- /dev/null +++ b/packaging/nix/module.nix @@ -0,0 +1,57 @@ +{ + config, + lib, + pkgs, + ... +}: let + cfg = config.services.villas.node; + + villasNodeCfg = pkgs.writeText "villas-node.conf" (builtins.toJSON cfg.config); +in + with lib; { + options = { + services.villas.node = { + enable = mkEnableOption (lib.mdDoc "VILLASnode is a client/server application to connect simulation equipment and software."); + + package = mkPackageOption pkgs "villas" {}; + + config = mkOption { + type = types.attrsOf types.anything; + default = { + nodes = {}; + paths = []; + idle_stop = false; # Do not stop daemon because of empty paths + }; + + description = lib.mdDoc '' + Contents of the VILLASnode configuration file, + {file}`villas-node.json`. + + See: https://villas.fein-aachen.org/docs/node/config/ + ''; + }; + }; + }; + + config = mkIf cfg.enable { + assertions = [ + { + assertion = cfg.config != null && cfg.config != {}; + message = "You must provide services.villas.node.config."; + } + ]; + + # configuration file indirection is needed to support reloading + environment.etc."villas/node.json".source = villasNodeCfg; + + systemd.services.villas-node = { + description = "VILLASnode"; + after = ["network.target"]; + wantedBy = ["multi-user.target"]; + serviceConfig = { + Type = "simple"; + ExecStart = "${lib.getExe cfg.package} node /etc/villas/node.json"; + }; + }; + }; + }