From 9bdcfb0f9b89240ddd7fafe125044cee6ce154ae Mon Sep 17 00:00:00 2001 From: James Andariese Date: Wed, 24 Jul 2024 10:18:10 -0500 Subject: [PATCH] add flake --- README.md | 25 ++++++++++++ flake.lock | 61 ++++++++++++++++++++++++++++ flake.nix | 45 +++++++++++++++++++++ nixos-module.nix | 103 +++++++++++++++++++++++++++++++++++++++++++++++ package.nix | 18 +++++++++ 5 files changed, 252 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 nixos-module.nix create mode 100644 package.nix diff --git a/README.md b/README.md index b429142..d9a0877 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,31 @@ Process Mutex Used to manage a lock and timing components of an at-most-once execution system. +## Flake Usage in NixOS + +Installing is accomplished with the included NixOS module. The included module +automatically enables the putex package system-wide so including it is sufficient. + +```nix +{ inputs = { + nixpkgs.url = "nixpkgs"; + putex.url = "git+https://git.strudelline.net/james/putex" <-- include the flake + }; + outputs = { + nixosConfigurations = { + container = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + putex.nixosModules.default # <-- then include the module. + { networking.useDHCP = false; boot.isContainer = true; system.stateVersion = "24.05"; } + ]; + }; + }; + }; +} +``` + + ### NATS This currently uses [NATS](https://nats.io) exclusively for locking but there is diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..89d82cb --- /dev/null +++ b/flake.lock @@ -0,0 +1,61 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1710146030, + "narHash": "sha256-SZ5L6eA7HJ/nmkzGG7/ISclqe6oZdOZTNoesiInkXPQ=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "b1d9ab70662946ef0850d488da1c9019f3a9752a", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1721838734, + "narHash": "sha256-o87oh2nLDzZ1E9+j1I6GaEvd9865OWGYvxaPSiH9DEU=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1855c9961e0bfa2e776fa4b58b7d43149eeed431", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable-small", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..2b6da30 --- /dev/null +++ b/flake.nix @@ -0,0 +1,45 @@ +{ + description = "CoreDNS"; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable-small"; + flake-utils.url = "github:numtide/flake-utils"; + }; + + outputs = {self, flake-utils, nixpkgs }: + with nixpkgs.lib; + let + packageConfigBase = flake-utils.lib.eachDefaultSystem (system: + let + pkgs = nixpkgs.legacyPackages.${system}; + pkg = pkgs.callPackage ./package.nix {}; + in + { + packages = rec { + putex = pkg; + default = pkg; + }; + } + ); + nixosModules = rec { + putex = import ./nixos-module.nix self nixpkgs.lib; + default = putex; + }; + nixosConfigurations = { + container = nixpkgs.lib.nixosSystem { + system = "x86_64-linux"; + modules = [ + self.nixosModules.default + { + networking.useDHCP = false; boot.isContainer = true; system.stateVersion = "24.05"; + + services.putex.putexes."testputex" = {}; + + } + ]; + }; + }; + in + packageConfigBase // { + inherit nixosModules nixosConfigurations; + }; +} diff --git a/nixos-module.nix b/nixos-module.nix new file mode 100644 index 0000000..c64ab34 --- /dev/null +++ b/nixos-module.nix @@ -0,0 +1,103 @@ +self: lib: + +with lib; + +{ config, ... }: + +let cfg = config.services.putex; + prog = config.programs.putex; + shq = lib.escapeShellArg; + shqs = lib.escapeShellArgs; +in + +let + #runCommand = cmd: derivation { name = "script"; builder = "/bin/sh"; args = [ "-c" cmd ]; }; + #writeExecutable = cmd: runCommand '' + # echo "${cmd}" > $out + # chmod +x $out + #''; + generateSystemdUnits = + name: { nats, bucket, key, token, healthcheck, stop, start, enable, package }: + { + services."putex-${name}" = { + script = '' + ${package}/bin/putex -vvv \ + --nats ${shq nats} \ + --bucket ${shq bucket} \ + --key ${shq key} \ + --token ${shq token} \ + --healthcheck ${shq healthcheck} \ + --start ${shq start} \ + --stop ${shq stop} + ''; + after = [ "network.target" ]; + wantedBy = [ "network.target" ]; + }; + }; +in + +{ + options = { + programs.putex = { + enable = mkEnableOption "putex" // { default = true; }; + package = mkPackageOption self.packages.${config.nixpkgs.system} "putex" { }; + }; + services.putex = { + putexes = mkOption { + type = types.attrsOf (types.submodule ({name, ...}: { + options = { + nats = mkOption { + description = "NATS address for cluster backing the lock"; + type = types.str; + default = "nats://127.0.0.1"; + }; + bucket = mkOption { + description = "bucket name in which to store the lock within which in"; + type = types.str; + default = "putexes"; + }; + key = mkOption { + description = "kv key name for lock (the lock's \"name\" in the bucket)"; + type = types.str; + default = name; + }; + token = mkOption { + description = "kv token value which is unique to this agent within the context of the key."; + type = types.str; + default = "${config.networking.hostName}"; + }; + healthcheck = mkOption { + description = "script which evaluates the ability of this agent to serve as the host"; + type = types.lines; + default = '' + ! systemctl is-failed + ''; + }; + stop = mkOption { + description = "script to start the service"; + type = types.lines; + default = '' + systemctl stop ${name}.service + ''; + }; + start = mkOption { + description = "script to start the service"; + type = types.lines; + default = '' + systemctl start ${name}.service + ''; + }; + enable = mkEnableOption "putex for ${name}" // { default = true; }; + package = mkPackageOption self.packages.${config.nixpkgs.system} "putex" { }; + }; + })); + }; + }; + }; + config = { + services.putex.putexes = {}; + environment.systemPackages = mkIf prog.enable [ prog.package ]; + systemd = mkMerge (attrValues (mapAttrs generateSystemdUnits cfg.putexes)); + }; +} + diff --git a/package.nix b/package.nix new file mode 100644 index 0000000..a4206d2 --- /dev/null +++ b/package.nix @@ -0,0 +1,18 @@ +{ lib, rustPlatform }: + +with builtins; +with lib; + +let cargoToml = (fromTOML (readFile ./Cargo.toml)); +in + +rustPlatform.buildRustPackage rec { + pname = cargoToml.package.name; + version = cargoToml.package.version; + src = ./.; + + cargoLock = { + lockFile = src + /Cargo.lock; + }; + +}