flake rework, dep updates

This commit is contained in:
Robin Appelman 2024-02-03 16:13:06 +01:00
commit 881c06173b
8 changed files with 738 additions and 521 deletions

954
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -7,10 +7,10 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
ssh2 = "0.9.3"
dotenv = "0.15.0"
ssh2 = "0.9.4"
dotenvy = "0.15.7"
main_error = "0.1.2"
tokio = { version = "1.20.1", features = ["macros", "rt-multi-thread"] }
warp = "0.3.2"
ctrlc = "3.2.2"
rumqttc = "0.12.0"
tokio = { version = "1.36.0", features = ["macros", "rt-multi-thread"] }
warp = "0.3.6"
ctrlc = "3.4.2"
rumqttc = "0.23.0"

72
flake.lock generated
View file

@ -1,67 +1,57 @@
{
"nodes": {
"flake-utils": {
"locked": {
"lastModified": 1656928814,
"narHash": "sha256-RIFfgBuKz6Hp89yRr7+NR5tzIAbn52h8vT6vXkYjZoM=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "7e2a3b3dfd9af950a856d66b0a7d01e3c18aa249",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"naersk": {
"inputs": {
"nixpkgs": "nixpkgs"
"systems": "systems"
},
"locked": {
"lastModified": 1655042882,
"narHash": "sha256-9BX8Fuez5YJlN7cdPO63InoyBy7dm3VlJkkmTt6fS1A=",
"owner": "nix-community",
"repo": "naersk",
"rev": "cddffb5aa211f50c4b8750adbec0bbbdfb26bb9f",
"lastModified": 1705309234,
"narHash": "sha256-uNRRNRKmJyCRC/8y1RqBkqWBLM034y4qN7EprSdmgyA=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "1ef2e671c3b0c19053962c07dbda38332dcebf26",
"type": "github"
},
"original": {
"owner": "nix-community",
"repo": "naersk",
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 0,
"narHash": "sha256-XZ1fKlEkAQvgocuBYq9lXEP3eNW6gF9JHsPVsvkWMhU=",
"path": "/nix/store/2xbq6yjs2lf2j0438h162mf66jvg0wyg-source",
"type": "path"
},
"original": {
"id": "nixpkgs",
"type": "indirect"
}
},
"nixpkgs_2": {
"locked": {
"lastModified": 0,
"narHash": "sha256-XZ1fKlEkAQvgocuBYq9lXEP3eNW6gF9JHsPVsvkWMhU=",
"path": "/nix/store/2xbq6yjs2lf2j0438h162mf66jvg0wyg-source",
"type": "path"
"lastModified": 1706956340,
"narHash": "sha256-g1hZv4OnW2UR6+OBWbeYB2kNnbq0W81wzZhOk/3yoXk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "7b9bd55d0f67ee214a8c9f3353ded09618ab51b2",
"type": "github"
},
"original": {
"id": "nixpkgs",
"ref": "release-23.11",
"type": "indirect"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"naersk": "naersk",
"nixpkgs": "nixpkgs_2"
"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"
}
}
},

121
flake.nix
View file

@ -1,126 +1,45 @@
{
inputs = {
nixpkgs.url = "nixpkgs/release-23.11";
flake-utils.url = "github:numtide/flake-utils";
naersk.url = "github:nix-community/naersk";
};
outputs = {
self,
nixpkgs,
flake-utils,
naersk,
}:
flake-utils.lib.eachDefaultSystem (
system: let
pkgs = nixpkgs.legacyPackages."${system}";
naersk-lib = naersk.lib."${system}";
overlays = [
(import ./overlay.nix)
];
pkgs = (import nixpkgs) {
inherit system overlays;
};
in rec {
# `nix build`
packages.wifi-prometheus-exporter = naersk-lib.buildPackage {
pname = "wifi-prometheus-exporter";
root = ./.;
nativeBuildInputs = [pkgs.pkg-config];
buildInputs = [pkgs.openssl];
packages = rec {
inherit (pkgs) wifi-prometheus-exporter;
default = wifi-prometheus-exporter;
};
defaultPackage = packages.wifi-prometheus-exporter;
defaultApp = packages.wifi-prometheus-exporter;
# `nix develop`
devShell = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo bacon cargo-edit cargo-outdated pkgs.openssl pkgs.pkg-config];
devShells.default = pkgs.mkShell {
nativeBuildInputs = with pkgs; [rustc cargo bacon cargo-edit cargo-outdated openssl pkg-config clippy];
};
}
)
// {
nixosModule = {
overlays.default = import ./overlay.nix;
nixosModules.default = {
pkgs,
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.wifi-prometheus-exporter;
in {
options.services.wifi-prometheus-exporter = {
enable = mkEnableOption "WiFi prometheus exporter";
sshAddress = mkOption {
type = types.str;
description = "ssh address of the wifi access point";
};
sshKeyFile = mkOption {
type = types.str;
description = "path containing the ssh private key";
};
sshPubKeyFile = mkOption {
type = types.str;
description = "path containing the ssh public key";
};
interfaces = mkOption {
type = types.listOf types.str;
description = "access point interface to expose";
};
port = mkOption {
type = types.int;
default = 9010;
description = "port to listen to";
};
mqttSecretFile = mkOption {
type = types.str;
description = "path containing MQTT_HOSTNAME, MQTT_USERNAME and MQTT_PASSWORD environment variables";
};
};
config = mkIf cfg.enable {
systemd.services."wifi-prometheus-exporter" = let
pkg = self.defaultPackage.${pkgs.system};
in {
wantedBy = ["multi-user.target"];
script = "${pkg}/bin/wifi-prometheus-exporter";
environment = {
PORT = toString cfg.port;
ADDR = cfg.sshAddress;
KEYFILE = cfg.sshKeyFile;
PUBFILE = cfg.sshPubKeyFile;
INTERFACES = concatStringsSep " " cfg.interfaces;
};
serviceConfig = {
EnvironmentFile = cfg.mqttSecretFile;
Restart = "on-failure";
DynamicUser = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
PrivateDevices = true;
ProtectClock = true;
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = "AF_INET AF_INET6";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "any";
IPAddressAllow = ["192.168.0.0/16" "localhost"];
PrivateUsers = true;
ProcSubset = "pid";
};
};
}: {
imports = [./module.nix];
config = lib.mkIf config.services.wifi-prometheus-exporter.enable {
nixpkgs.overlays = [self.overlays.default];
services.wifi-prometheus-exporter.package = lib.mkDefault pkgs.wifi-prometheus-exporter;
};
};
};

94
module.nix Normal file
View file

@ -0,0 +1,94 @@
{
config,
lib,
pkgs,
...
}:
with lib; let
cfg = config.services.wifi-prometheus-exporter;
in {
options.services.wifi-prometheus-exporter = {
enable = mkEnableOption "WiFi prometheus exporter";
sshAddress = mkOption {
type = types.str;
description = "ssh address of the wifi access point";
};
sshKeyFile = mkOption {
type = types.str;
description = "path containing the ssh private key";
};
sshPubKeyFile = mkOption {
type = types.str;
description = "path containing the ssh public key";
};
interfaces = mkOption {
type = types.listOf types.str;
description = "access point interface to expose";
};
port = mkOption {
type = types.int;
default = 9010;
description = "port to listen to";
};
mqttSecretFile = mkOption {
type = types.str;
description = "path containing MQTT_HOSTNAME, MQTT_USERNAME and MQTT_PASSWORD environment variables";
};
package = mkOption {
type = types.package;
defaultText = literalExpression "pkgs.dispenser";
description = "package to use";
};
};
config = mkIf cfg.enable {
systemd.services."wifi-prometheus-exporter" = {
wantedBy = ["multi-user.target"];
environment = {
PORT = toString cfg.port;
ADDR = cfg.sshAddress;
KEYFILE = cfg.sshKeyFile;
PUBFILE = cfg.sshPubKeyFile;
INTERFACES = concatStringsSep " " cfg.interfaces;
};
serviceConfig = {
ExecStart = "${pkgs.wifi-prometheus-exporter}/bin/wifi-prometheus-exporter";
EnvironmentFile = cfg.mqttSecretFile;
Restart = "on-failure";
DynamicUser = true;
PrivateTmp = true;
ProtectSystem = "strict";
ProtectHome = true;
NoNewPrivileges = true;
PrivateDevices = true;
ProtectClock = true;
CapabilityBoundingSet = true;
ProtectKernelLogs = true;
ProtectControlGroups = true;
SystemCallArchitectures = "native";
ProtectKernelModules = true;
RestrictNamespaces = true;
MemoryDenyWriteExecute = true;
ProtectHostname = true;
LockPersonality = true;
ProtectKernelTunables = true;
RestrictAddressFamilies = "AF_INET AF_INET6";
RestrictRealtime = true;
ProtectProc = "noaccess";
SystemCallFilter = ["@system-service" "~@resources" "~@privileged"];
IPAddressDeny = "any";
IPAddressAllow = ["192.168.0.0/16" "localhost"];
PrivateUsers = true;
ProcSubset = "pid";
};
};
};
}

3
overlay.nix Normal file
View file

@ -0,0 +1,3 @@
final: prev: {
wifi-prometheus-exporter = final.callPackage ./package.nix {};
}

29
package.nix Normal file
View file

@ -0,0 +1,29 @@
{
rustPlatform,
openssl,
pkg-config,
lib,
}: let
inherit (lib.sources) sourceByRegex;
src = sourceByRegex ./. ["Cargo.*" "(src)(/.*)?"];
in
rustPlatform.buildRustPackage rec {
pname = "wifi-prometheus-exporter";
version = "0.1.0";
inherit src;
buildInputs = [
openssl
];
nativeBuildInputs = [
pkg-config
];
doCheck = false;
cargoLock = {
lockFile = ./Cargo.lock;
};
}

View file

@ -66,7 +66,7 @@ impl WifiLister {
#[tokio::main]
async fn main() -> Result<(), MainError> {
let mut env: HashMap<String, String> = dotenv::vars().collect();
let mut env: HashMap<String, String> = dotenvy::vars().collect();
let addr = env.remove("ADDR").ok_or("No ADDR set")?;
let keyfile = env.remove("KEYFILE").ok_or("No KEYFILE set")?;
let pubfile = env.remove("PUBFILE").ok_or("No PUBFILE set")?;
@ -226,7 +226,7 @@ async fn send_update(
mac: String,
update: Update,
) -> Result<(), ClientError> {
let mac = mac.replace(":", "_");
let mac = mac.replace(':', "_");
match update {
Update::New => {
client