From 95ae2e9db2b1405f2e12bf1f3ed1986a049b9a82 Mon Sep 17 00:00:00 2001 From: markoburcul Date: Fri, 10 Jan 2025 12:13:30 +0100 Subject: [PATCH] nix: simplified codex service definition configObject option is used to define the configuration which is then dumped to a toml file and is used as input to codex binary(conditionally). Signed-off-by: markoburcul --- flake.nix | 32 +++++++++++++++++++++++++++----- nix/default.nix | 12 +++++++++--- nix/{codex.nix => service.nix} | 30 ++++++++++++++---------------- 3 files changed, 50 insertions(+), 24 deletions(-) rename nix/{codex.nix => service.nix} (55%) diff --git a/flake.nix b/flake.nix index 22c2838e..87246185 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,5 @@ { - description = "Codex build flake"; + description = "Nim Codex build flake"; inputs = { nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.11"; @@ -26,18 +26,40 @@ }; build = targets: buildTarget.override { inherit targets; }; in rec { - codex = build ["all"]; - default = codex; + nim-codex = build ["all"]; + default = nim-codex; }); - nixosModules.codex = import ./nix/codex.nix; + nixosModules.nim-codex = import ./nix/service.nix; + + checks = forAllSystems (system: let + pkgs = pkgsFor.${system}; + in { + nim-codex-test = pkgs.nixosTest { + name = "nim-codex-test"; + nodes = { + server = { config, pkgs, ... }: { + imports = [ self.nixosModules.nim-codex ]; + services.nim-codex.enable = true; + services.nim-codex.dataDir = "/var/lib/nim-codex-test"; + }; + }; + testScript = '' + print("Starting test: nim-codex-test") + machine.start() + machine.wait_for_unit("nim-codex.service") + machine.succeed("test -d /var/lib/nim-codex-test") + machine.wait_until_succeeds("journalctl -u nim-codex.service | grep 'Started codex node'", 10) + ''; + }; + }); devShells = forAllSystems (system: let pkgs = pkgsFor.${system}; in { default = pkgs.mkShell { inputsFrom = [ - packages.${system}.codex + packages.${system}.nim-codex circom-compat.packages.${system}.default ]; # Not using buildInputs to override fakeGit and fakeCargo. diff --git a/nix/default.nix b/nix/default.nix index 63970106..51337382 100644 --- a/nix/default.nix +++ b/nix/default.nix @@ -25,11 +25,17 @@ let tools = callPackage ./tools.nix {}; in pkgs.gcc11Stdenv.mkDerivation rec { - pname = "codex"; + pname = "nim-codex"; version = "${tools.findKeyValue "version = \"([0-9]+\.[0-9]+\.[0-9]+)\"" ../codex.nimble}-${revision}"; - inherit src; + src = pkgs.fetchFromGitHub { + owner = "codex-storage"; + repo = "nim-codex"; + rev = "HEAD"; + sha256 = "sha256-cPQDV46Z9z27Hd32eW726fC3J1dAhXyljbhAgFXVEXQ="; + fetchSubmodules = true; + }; # Dependencies that should exist in the runtime environment. buildInputs = with pkgs; [ @@ -76,7 +82,7 @@ in pkgs.gcc11Stdenv.mkDerivation rec { ''; meta = with pkgs.lib; { - description = "Codex storage system"; + description = "Nim Codex storage system"; homepage = "https://github.com/codex-storage/nim-codex"; license = licenses.mit; platforms = stableSystems; diff --git a/nix/codex.nix b/nix/service.nix similarity index 55% rename from nix/codex.nix rename to nix/service.nix index 5fa1c21e..2b3bab9c 100644 --- a/nix/codex.nix +++ b/nix/service.nix @@ -6,27 +6,21 @@ let toml = pkgs.formats.toml { }; - cfg = config.services.codex; + cfg = config.services.nim-codex; in { options = { - services.codex = { - enable = mkEnableOption "Codex Node service."; + services.nim-codex = { + enable = mkEnableOption "Nim Codex Node service."; package = mkOption { type = types.package; default = pkgs.callPackage ./default.nix { }; - defaultText = literalExpression "pkgs.codex"; - description = lib.mdDoc "Package to use as Codex node."; + defaultText = literalExpression "pkgs.nim-codex"; + description = lib.mdDoc "Package to use as Nim Codex node."; }; - configFile = mkOption { - type = types.nullOr types.str; - default = null; - description = "Path to the Codex configuration file."; - }; - - configObject = lib.mkOption { + settings = lib.mkOption { default = { }; type = toml.type; description = ''Structured settings object that will be used to generate a TOML config file.''; @@ -36,10 +30,10 @@ in config = mkIf cfg.enable { environment.etc = { - "codex/config.toml".source = toml.generate "config.toml" cfg.configObject; + "nim-codex/config.toml".source = toml.generate "config.toml" cfg.settings; }; - systemd.services.codex = { - description = "Codex Node"; + systemd.services.nim-codex = { + description = "Nim Codex Node"; wantedBy = [ "multi-user.target" ]; requires = [ "network.target" ]; serviceConfig = { @@ -50,9 +44,13 @@ in NoNewPrivileges = true; PrivateDevices = true; MemoryDenyWriteExecute = true; - ExecStart = ''${cfg.package}/bin/codex --config-file=${if cfg.configFile != null then cfg.configFile else "/etc/codex/config.toml"}''; + ExecStart = "${cfg.package}/bin/nim-codex --config-file=/etc/nim-codex/config.toml"; Restart = "on-failure"; }; + restartIfChanged = true; + restartTriggers = [ + "/etc/nim-codex/config.toml" + ]; }; }; }