Skip to content

Commit

Permalink
Merge pull request #179 from ralexstokes/optional-nixos-module
Browse files Browse the repository at this point in the history
refactor nixos module into "submodules" per component
  • Loading branch information
ralexstokes authored Nov 2, 2023
2 parents 24b4495 + e8ae593 commit 40c405a
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 65 deletions.
9 changes: 0 additions & 9 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,5 @@
devShells.x86_64-darwin.default = import ./shell.nix { pkgs = pkgs-for-system "x86_64-darwin"; };
devShells.aarch64-darwin.default = import ./shell.nix { pkgs = pkgs-for-system "aarch64-darwin"; };
devShells.x86_64-linux.default = import ./shell.nix { pkgs = pkgs-for-system "x86_64-linux"; };

# exercise defined modules in a "test machine"
nixosConfigurations.test = nixpkgs.lib.nixosSystem {
system = "x86_64-linux";
modules = [
./nix/module-test.nix
self.nixosModules.mev-rs
];
};
};
}
8 changes: 5 additions & 3 deletions nix/module-test.nix
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# NOTE: currently doesn't do what we want...
{
services.mev-rs = {
enable = "build";
config-file = "example.config.toml";
features = "build,config";
build = {
config-file = "example.config.toml";
additional-features = [ "config" ];
};
};

boot.isContainer = true;
Expand Down
123 changes: 70 additions & 53 deletions nix/module.nix
Original file line number Diff line number Diff line change
@@ -1,66 +1,83 @@
pkg:
{ config, lib, pkgs, ... }:
let
cfg = config.services.mev-rs;

component = cfg.enable;
# ensure the intended component is part of the feature set
features = if lib.strings.hasInfix component cfg.features then cfg.features else lib.strings.removePrefix "," "${cfg.features},${component}";

mev-rs = pkg.mev-rs {
mev-rs-with-features = features: pkg.mev-rs {
inherit features;
system = pkgs.system;
};

name = "mev-${cfg.enable}-rs";

cmd = ''
${mev-rs}/bin/mev \
${component} \
${cfg.config-file}
'';
in
{
options.services.mev-rs = {
enable = lib.mkOption {
type = lib.types.enum [ "boost" "build" ];
description = "which subcommand of `mev-rs` to run";
};
config-file = lib.mkOption {
type = lib.types.str;
description = ''
path to a config file suitable for the `mev-rs` toolkit
'';
};
features = lib.mkOption {
type = lib.types.str;
default = cfg.enable;
description = ''
feature set (comma-separated) to enable for `cargo` build
mev-rs-submodule = for-component: { config, ... }: with lib; with types;
let
features = strings.concatStringsSep "," [ for-component ] ++ config.additional-features;
mev-rs = mev-rs-with-features features;
name = "mev-${for-component}-rs";
cmd = ''
${mev-rs}/bin/mev \
${for-component} \
${config.config-file}
'';
};
};

config = {
networking.firewall = lib.mkIf (component == "build") {
allowedTCPPorts = [ 30303 ];
allowedUDPPorts = [ 30303 ];
};

environment.systemPackages = [
mev-rs
];
in
{
options = {
component = mkOption {
type = enum [ "boost" "relay" "build" ];
default = for-component;
};
config-file = mkOption {
type = str;
description = ''
path to a config file suitable for the `mev-rs` toolkit
'';
};
additional-features = mkOption {
type = listOf str;
description = ''
additional Cargo features to include alongside those required for the `component`
'';
};
after-systemd-service = mkOption {
type = str;
default = "vc.service";
description = ''
the name of a systemd service to wait activation of before activating this unit
'';
};
};
config = {
environment.systemPackages = [
mev-rs
];
networking.firewall = lib.mkIf (for-component == "build") {
allowedTCPPorts = [ 30303 ];
allowedUDPPorts = [ 30303 ];
};

systemd.services."${name}" = {
description = name;
wantedBy = [ "multi-user.target" ];
after = [ "vc.service" ];
serviceConfig = {
ExecStart = cmd;
Restart = "on-failure";
RestartSec = "10s";
SyslogIdentifier = name;
systemd.services."${name}" = {
description = name;
wantedBy = [ "multi-user.target" ];
after = [ config.after-systemd-service ];
serviceConfig = {
ExecStart = cmd;
Restart = "on-failure";
RestartSec = "10s";
SyslogIdentifier = for-component;
};
};
};
};
in
{
options.services.mev-rs = with lib; with types; mkOption {
type = listOf (submodule {
options.boost = mkOption {
type = attrsOf (submodule mev-rs-submodule "boost");
};
options.relay = mkOption {
type = attrsOf (submodule mev-rs-submodule "relay");
};
options.build = mkOption {
type = attrsOf (submodule mev-rs-submodule "build");
};
});
};
}

0 comments on commit 40c405a

Please sign in to comment.