Skip to content

Commit

Permalink
nix: simplified codex service definition
Browse files Browse the repository at this point in the history
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 <[email protected]>
markoburcul committed Jan 10, 2025
1 parent 08d7fbd commit 95ae2e9
Showing 3 changed files with 50 additions and 24 deletions.
32 changes: 27 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -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.
12 changes: 9 additions & 3 deletions nix/default.nix
Original file line number Diff line number Diff line change
@@ -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;
30 changes: 14 additions & 16 deletions nix/codex.nix → nix/service.nix
Original file line number Diff line number Diff line change
@@ -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"
];
};
};
}

0 comments on commit 95ae2e9

Please sign in to comment.