From 5d15da63502ee72d348923cc149fec7b735caaa5 Mon Sep 17 00:00:00 2001 From: Shea Levy Date: Fri, 10 May 2024 08:32:21 -0400 Subject: [PATCH] deploy: Add NixOS module (#34) --- flake.nix | 3 +++ nix/nixos.nix | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 nix/nixos.nix diff --git a/flake.nix b/flake.nix index 7a471929..2f5da683 100644 --- a/flake.nix +++ b/flake.nix @@ -19,6 +19,9 @@ inherit inputs; repoRoot = ./.; systems = [ "x86_64-linux" "x86_64-darwin" ]; + flake = { + nixosModules.default = import ./nix/nixos.nix inputs.self; + }; outputs = import ./nix/outputs.nix; }; diff --git a/nix/nixos.nix b/nix/nixos.nix new file mode 100644 index 00000000..c5ca374c --- /dev/null +++ b/nix/nixos.nix @@ -0,0 +1,64 @@ +self: { lib, config, pkgs, ... }: +let + inherit (lib) mkOption types mapAttrs'; + + inherit (pkgs) writeTextDir symlinkJoin; + + runnerOptions = { name, ... }: { + options = { + domain = mkOption { + type = types.str; + default = name; + description = "The domain to host marlowe-runner"; + }; + + # TODO local or remote + runtime-instance = mkOption { + type = types.str; + description = "The name of the runtime instance to connect to"; + }; + + flake = mkOption { + type = types.attrs; + default = self; + description = "A Nix Flake containing the runner application"; + }; + }; + }; + + mkRoot = name: { runtime-instance, flake, ... }: + let + configJson = writeTextDir "config.json" (builtins.toJSON { + marloweWebServerUrl = "//${config.marlowe.runtimes.${runtime-instance}.domain}"; + develMode = false; + }); + in + symlinkJoin { + name = "marlowe-runner-${name}-root"; + paths = [ + flake.packages.${pkgs.system}.marlowe-runner + configJson + ]; + }; +in +{ + options = { + marlowe.runners = mkOption { + type = types.attrsOf (types.submodule runnerOptions); + default = { }; + description = "Marlowe Runner instances to run"; + }; + }; + config = { + http-services.static-sites = mapAttrs' + (name: runner: { + name = "marlowe-runner-${name}"; + value = { + inherit (runner) domain; + root = mkRoot name runner; + index-fallback = true; + }; + }) + config.marlowe.runners; + }; +}