add flake

This commit is contained in:
James Andariese 2024-07-24 10:18:10 -05:00
parent b7cba1fb84
commit 9bdcfb0f9b
5 changed files with 252 additions and 0 deletions

View File

@ -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

61
flake.lock Normal file
View File

@ -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
}

45
flake.nix Normal file
View File

@ -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;
};
}

103
nixos-module.nix Normal file
View File

@ -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));
};
}

18
package.nix Normal file
View File

@ -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;
};
}