From 951adbd416cf40cf89940314d5306555bd0b784d Mon Sep 17 00:00:00 2001 From: Nicolas Henin Date: Fri, 7 Jun 2024 11:01:27 +0200 Subject: [PATCH] nix formatting --- nix/nixos.nix | 246 ++++++++++++++++++++++++++++---------------------- 1 file changed, 137 insertions(+), 109 deletions(-) diff --git a/nix/nixos.nix b/nix/nixos.nix index c62a9aa0ce..ec05e2e245 100644 --- a/nix/nixos.nix +++ b/nix/nixos.nix @@ -1,4 +1,5 @@ -{ self, marlowe-plutus, cardano-node, ... }: { lib, config, pkgs, ... }: let +{ self, marlowe-plutus, cardano-node, ... }: { lib, config, pkgs, ... }: +let inherit (lib) mkOption types mkIf attrNames listToAttrs imap0 foldr attrValues mapAttrs' makeBinPath concatStringsSep optionalAttrs mkForce optionals versionAtLeast; allowedEnvs = [ "preprod" "preview" "mainnet" ]; @@ -34,7 +35,7 @@ cfg = config.marlowe.runtimes; - neededEnvs = attrNames (foldr (x: acc: acc // { ${x.network} = null; }) {} (attrValues cfg)); + neededEnvs = attrNames (foldr (x: acc: acc // { ${x.network} = null; }) { } (attrValues cfg)); mkNode = env: { autoStart = true; @@ -52,7 +53,8 @@ }; }; }; -in { +in +{ options = { marlowe.runtimes = mkOption { type = types.attrsOf (types.submodule runtimeOptions); @@ -68,38 +70,45 @@ in { }; config = mkIf (cfg != { }) { - http-services.proxied-services = listToAttrs (imap0 (idx: name: let runtime = cfg.${name}; in { - name = "marlowe-runtime-${name}-web"; - value = { - inherit (runtime) domain; - systemdConfig = port: { - description = "Marlowe Runtime Web (${name})"; - # allow unlimited restarts - unitConfig.StartLimitIntervalSec = 0; - serviceConfig = { - # Restart on exit, with exponential fallback up to an hour - # runtime-web needs to wait for runtime, which can take arbitrarily - # long to be ready (see comment there). - Restart = "always"; - RestartMaxDelaySec= "1hour"; - RestartSteps = 10; - - ExecSearchPath = makeBinPath [ runtime.flake.packages.${pkgs.system}.marlowe-web-server ]; - DynamicUser = true; - ExecStart = concatStringsSep " " [ - "marlowe-web-server" - "--http-port" (toString port) - "--marlowe-runtime-host" "127.0.0.1" - "--marlowe-runtime-port" (toString (3701 + 3 * idx)) - "--enable-open-api" - "--access-control-allow-origin-all" - ]; + http-services.proxied-services = listToAttrs (imap0 + (idx: name: + let runtime = cfg.${name}; in + { + name = "marlowe-runtime-${name}-web"; + value = { + inherit (runtime) domain; + systemdConfig = port: { + description = "Marlowe Runtime Web (${name})"; + # allow unlimited restarts + unitConfig.StartLimitIntervalSec = 0; + serviceConfig = { + # Restart on exit, with exponential fallback up to an hour + # runtime-web needs to wait for runtime, which can take arbitrarily + # long to be ready (see comment there). + Restart = "always"; + RestartMaxDelaySec = "1hour"; + RestartSteps = 10; + + ExecSearchPath = makeBinPath [ runtime.flake.packages.${pkgs.system}.marlowe-web-server ]; + DynamicUser = true; + ExecStart = concatStringsSep " " [ + "marlowe-web-server" + "--http-port" + (toString port) + "--marlowe-runtime-host" + "127.0.0.1" + "--marlowe-runtime-port" + (toString (3701 + 3 * idx)) + "--enable-open-api" + "--access-control-allow-origin-all" + ]; + }; + requires = [ "marlowe-runtime-${name}.service" ]; + after = [ "marlowe-runtime-${name}.service" ]; + }; }; - requires = [ "marlowe-runtime-${name}.service" ]; - after = [ "marlowe-runtime-${name}.service" ]; - }; - }; - }) (attrNames cfg)); + }) + (attrNames cfg)); services.postgresql = { enable = true; ensureDatabases = map (inst: "runtime-${inst}") (attrNames cfg); @@ -125,80 +134,99 @@ in { }; }; - systemd.services = listToAttrs (imap0 (idx: name: let - inherit (pkgs) writeShellApplication sqitchPg z3 postgresql; - runtimeCfg = cfg.${name}; - env = runtimeCfg.network; - envConfig = config.containers."cardano-node-${env}".config.services.cardano-node.environments.${env}; - db = "postgresql://postgres@/runtime-${name}"; - sqitch-conf = builtins.toFile "sqitch.conf" '' - [user] - name = chainindexer - email = example@example.com - ''; - runSqitch = writeShellApplication { - name = "run-sqitch"; - text = '' - export SQITCH_USER_CONFIG=${sqitch-conf} - export TZ=Etc/UTC - - cd ${runtimeCfg.flake.sqitch-plan-dirs.chain-sync} - sqitch --quiet deploy --target ${db} - - cd ${runtimeCfg.flake.sqitch-plan-dirs.runtime} - sqitch --quiet deploy --target ${db} - ''; - runtimeInputs = [ sqitchPg postgresql ]; - }; - pkg = runtimeCfg.flake.packages.${pkgs.system}.marlowe-runtime; - in { - name = "marlowe-runtime-${name}"; - value = { - description = "Marlowe Runtime (${name})"; - # allow unlimited restarts - unitConfig.StartLimitIntervalSec = 0; - serviceConfig = { - # Restart on exit, with exponential fallback up to an hour - # The runtime needs to wait for postgres and the node to be up (which - # should be relatively quick) /and/ for the node to be in Babbage (which - # can take an arbitrarily long time). - Restart = "always"; - RestartMaxDelaySec= "1hour"; - RestartSteps = 10; - - ExecSearchPath = makeBinPath [ runSqitch pkg ]; - # TODO: Wait for the nodes to be in babbage to start - ExecStartPre = "run-sqitch"; - ExecStart = concatStringsSep " " ([ - "marlowe-runtime" - "--socket-path" "/var/lib/nixos-containers/cardano-node-${env}/var/lib/cardano-node/node.socket" - "--database-uri" db - "--shelley-genesis-config-file" envConfig.networkConfig.ShelleyGenesisFile - "--genesis-config-file" envConfig.networkConfig.ByronGenesisFile - "--genesis-config-file-hash" envConfig.networkConfig.ByronGenesisHash - "--store-dir" "/var/lib/marlowe-runtime-${name}/store" - "--host" "0.0.0.0" - "--port" (toString (3700 + 3 * idx)) - "--port-traced" (toString (3701 + 3 * idx)) - "--http-port" (toString (3702 + 3 * idx)) - "--minting-policy-cmd" "marlowe-minting-validator" - ] ++ optionals (versionAtLeast pkg.version "0.6") [ - "--min-contract-age" "87400s" - "--max-store-size" "8589934592" - ]); - }; - requires = [ "container@cardano-node-${env}.service" "postgresql.service" ]; - after = [ "container@cardano-node-${env}.service" "postgresql.service" ]; - environment = optionalAttrs (envConfig.networkConfig.RequiresNetworkMagic == "RequiresMagic") { - CARDANO_TESTNET_MAGIC = toString (builtins.fromJSON (builtins.readFile envConfig.networkConfig.ShelleyGenesisFile)).networkMagic; - }; - path = [ z3 runtimeCfg.marlowe-plutus-flake.packages.${pkgs.system}.marlowe-minting-validator ]; - }; - }) (attrNames cfg)); - - containers = listToAttrs (map (env: { - name = "cardano-node-${env}"; - value = mkNode env; - }) neededEnvs); + systemd.services = listToAttrs (imap0 + (idx: name: + let + inherit (pkgs) writeShellApplication sqitchPg z3 postgresql; + runtimeCfg = cfg.${name}; + env = runtimeCfg.network; + envConfig = config.containers."cardano-node-${env}".config.services.cardano-node.environments.${env}; + db = "postgresql://postgres@/runtime-${name}"; + sqitch-conf = builtins.toFile "sqitch.conf" '' + [user] + name = chainindexer + email = example@example.com + ''; + runSqitch = writeShellApplication { + name = "run-sqitch"; + text = '' + export SQITCH_USER_CONFIG=${sqitch-conf} + export TZ=Etc/UTC + + cd ${runtimeCfg.flake.sqitch-plan-dirs.chain-sync} + sqitch --quiet deploy --target ${db} + + cd ${runtimeCfg.flake.sqitch-plan-dirs.runtime} + sqitch --quiet deploy --target ${db} + ''; + runtimeInputs = [ sqitchPg postgresql ]; + }; + pkg = runtimeCfg.flake.packages.${pkgs.system}.marlowe-runtime; + in + { + name = "marlowe-runtime-${name}"; + value = { + description = "Marlowe Runtime (${name})"; + # allow unlimited restarts + unitConfig.StartLimitIntervalSec = 0; + serviceConfig = { + # Restart on exit, with exponential fallback up to an hour + # The runtime needs to wait for postgres and the node to be up (which + # should be relatively quick) /and/ for the node to be in Babbage (which + # can take an arbitrarily long time). + Restart = "always"; + RestartMaxDelaySec = "1hour"; + RestartSteps = 10; + + ExecSearchPath = makeBinPath [ runSqitch pkg ]; + # TODO: Wait for the nodes to be in babbage to start + ExecStartPre = "run-sqitch"; + ExecStart = concatStringsSep " " ([ + "marlowe-runtime" + "--socket-path" + "/var/lib/nixos-containers/cardano-node-${env}/var/lib/cardano-node/node.socket" + "--database-uri" + db + "--shelley-genesis-config-file" + envConfig.networkConfig.ShelleyGenesisFile + "--genesis-config-file" + envConfig.networkConfig.ByronGenesisFile + "--genesis-config-file-hash" + envConfig.networkConfig.ByronGenesisHash + "--store-dir" + "/var/lib/marlowe-runtime-${name}/store" + "--host" + "0.0.0.0" + "--port" + (toString (3700 + 3 * idx)) + "--port-traced" + (toString (3701 + 3 * idx)) + "--http-port" + (toString (3702 + 3 * idx)) + "--minting-policy-cmd" + "marlowe-minting-validator" + ] ++ optionals (versionAtLeast pkg.version "0.6") [ + "--min-contract-age" + "87400s" + "--max-store-size" + "8589934592" + ]); + }; + requires = [ "container@cardano-node-${env}.service" "postgresql.service" ]; + after = [ "container@cardano-node-${env}.service" "postgresql.service" ]; + environment = optionalAttrs (envConfig.networkConfig.RequiresNetworkMagic == "RequiresMagic") { + CARDANO_TESTNET_MAGIC = toString (builtins.fromJSON (builtins.readFile envConfig.networkConfig.ShelleyGenesisFile)).networkMagic; + }; + path = [ z3 runtimeCfg.marlowe-plutus-flake.packages.${pkgs.system}.marlowe-minting-validator ]; + }; + }) + (attrNames cfg)); + + containers = listToAttrs (map + (env: { + name = "cardano-node-${env}"; + value = mkNode env; + }) + neededEnvs); }; }