From 62631bcae4fd99c066f719a92e24fb201f1d9129 Mon Sep 17 00:00:00 2001 From: 4o1x5 <4o1x5@4o1x5.dev> Date: Tue, 21 May 2024 00:16:36 +0200 Subject: [PATCH] flake finished --- Cargo.lock | 2 +- Cargo.toml | 2 +- flake.lock | 107 ++----------------------------- flake.nix | 181 +++++++++++++++++++++++++++++++++++------------------ 4 files changed, 128 insertions(+), 164 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a04def8..17f7e49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -733,7 +733,7 @@ dependencies = [ ] [[package]] -name = "i2pdmetrics" +name = "i2pd-exporter" version = "0.1.0" dependencies = [ "async-std", diff --git a/Cargo.toml b/Cargo.toml index 41d94fb..01aa0fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "i2pdmetrics" +name = "i2pd-exporter" version = "0.1.0" edition = "2021" diff --git a/flake.lock b/flake.lock index f28a218..d0ea0f7 100644 --- a/flake.lock +++ b/flake.lock @@ -1,119 +1,24 @@ { "nodes": { - "advisory-db": { - "flake": false, - "locked": { - "lastModified": 1714183630, - "narHash": "sha256-1BVft7ggSN2XXFeXQjazU3jN9wVECd9qp2mZx/8GDMk=", - "owner": "rustsec", - "repo": "advisory-db", - "rev": "35e7459a331d3e0c585e56dabd03006b9b354088", - "type": "github" - }, - "original": { - "owner": "rustsec", - "repo": "advisory-db", - "type": "github" - } - }, - "crane": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ] - }, - "locked": { - "lastModified": 1716155989, - "narHash": "sha256-waAI5EvvISkQyw44awtrzdzqTVKsrSLbgiUHP6RM6BE=", - "owner": "ipetkov", - "repo": "crane", - "rev": "b7a1655564f96c28fa4c7a0d4888034c47f5ceb0", - "type": "github" - }, - "original": { - "owner": "ipetkov", - "repo": "crane", - "type": "github" - } - }, - "fenix": { - "inputs": { - "nixpkgs": [ - "nixpkgs" - ], - "rust-analyzer-src": [] - }, - "locked": { - "lastModified": 1716099865, - "narHash": "sha256-GrNswS37mF+Jj/GNb2uNapd11sR9IWf7j9WexybunPs=", - "owner": "nix-community", - "repo": "fenix", - "rev": "f7737feef42fa8abe70de20b9a13b845a113cfeb", - "type": "github" - }, - "original": { - "owner": "nix-community", - "repo": "fenix", - "type": "github" - } - }, - "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": 1716128955, - "narHash": "sha256-3DNg/PV+X2V7yn8b/fUR2ppakw7D9N4sjVBGk6nDwII=", + "lastModified": 1716137900, + "narHash": "sha256-sowPU+tLQv8GlqtVtsXioTKeaQvlMz/pefcdwg8MvfM=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "f9256de8281f2ccd04985ac5c30d8f69aefadbe8", + "rev": "6c0b7a92c30122196a761b440ac0d46d3d9954f1", "type": "github" }, "original": { - "owner": "NixOS", - "ref": "nixpkgs-unstable", - "repo": "nixpkgs", - "type": "github" + "id": "nixpkgs", + "ref": "nixos-unstable", + "type": "indirect" } }, "root": { "inputs": { - "advisory-db": "advisory-db", - "crane": "crane", - "fenix": "fenix", - "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", diff --git a/flake.nix b/flake.nix index 791e6e0..73a5698 100644 --- a/flake.nix +++ b/flake.nix @@ -1,72 +1,131 @@ { - inputs = { - naersk.url = "github:nix-community/naersk/master"; - nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; - utils.url = "github:numtide/flake-utils"; - }; + description = "i2pd-exporter devshell, service and options"; - outputs = { self, nixpkgs, utils, naersk }: - utils.lib.eachDefaultSystem (system: - let - pkgs = import nixpkgs { inherit system; }; - naersk-lib = pkgs.callPackage naersk { }; - in - { - defaultPackage = naersk-lib.buildPackage ./.; - devShell = with pkgs; mkShell { - buildInputs = [ cargo rustc rustfmt pre-commit rustPackages.clippy ]; - RUST_SRC_PATH = rustPlatform.rustLibSrc; - # todo add rust for vscodium - }; + # Nixpkgs / NixOS version to use. + inputs.nixpkgs.url = "nixpkgs/nixos-unstable"; + + outputs = { self, nixpkgs }: + let + #version = builtins.substring 0 8 self.lastModifiedDate; + + # System types to support. + supportedSystems = + [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ]; + + forAllSystems = nixpkgs.lib.genAttrs supportedSystems; + + nixpkgsFor = forAllSystems (system: import nixpkgs { + inherit system; + }); + + in + { + + packages = forAllSystems (system: + let + pkgs = nixpkgsFor.${system}; + in + { + default = pkgs.rustPlatform.buildRustPackage { + pname = "i2pd-exporter"; + version = "0.1.1"; + src = ./.; + + nativeBuildInputs = with pkgs; [ + pkg-config + openssl + ]; + buildInputs = with pkgs; [ + pkg-config + openssl + ]; + cargoLock.lockFile = ./Cargo.lock; + }; + }); + + nixosModules.default = { config, lib, pkgs, ... }: + with lib; + let + cfg = config.services.prometheus.exporters.i2pd; + in + { + options.services.prometheus.exporters.i2pd = { + + enable = mkEnableOption "Enable the i2pd exporter"; + + listenAddress = mkOption { + type = types.str; + default = "127.0.0.1"; + }; + + port = mkOption { + type = types.port; + default = 5733; + }; + + routerAddress = mkOption { + type = types.str; + default = "http://127.0.0.1:7650"; + }; + + openFirewall = mkOption { + type = types.bool; + default = false; + description = lib.mdDoc '' + Open the appropriate ports in the firewall for i2pd-exporter. + ''; + }; + + routerPassword = mkOption { + type = types.str; + default = "itoopie"; + }; + }; - nixosModules.default = - { pkgs, config, lib, ... }: - { - options = { - services.i2pd-exporter = with lib; { + config = mkIf cfg.enable { + systemd.services.i2pd-exporter = { + description = "Exporter for i2pd"; + wantedBy = [ "multi-user.target" ]; + environment = { + IP = cfg.listenAddress; + PORT = toString cfg.port; - enable = mkEnableOption "i2pd-exporter"; + ROUTER = cfg.routerAddress; + PASSWORD = cfg.routerPassword; + }; - listenAddress = mkOption { - type = with types; nullOr str; - default = "127.0.0.1"; - }; - - port = mkOption { - type = types.port; - default = 5733; - }; - - routerAddress = mkOption { - type = with types; nullOr str; - default = "http://127.0.0.1:7650"; - }; - - routerPassword = mkOption { - type = with types; nullOr str; - default = "itoopie"; - }; + serviceConfig = { + DynamicUser = "yes"; + ExecStart = "${self.packages.${pkgs.system}.default}/bin/i2pd-exporter"; + Restart = "on-failure"; + RestartSec = "5s"; }; }; - config = - let - cfg = config.services.i2pd-exporter; - in - lib.mkIf cfg.enable { - systemd.services.i2pd-exporter = { - environment = { - IP = cfg.listenAddress; - PORT = cfg.port; - ADDRESS = cfg.routerAddress; - PASSWORD = cfg.routerPassword; - }; - wantedBy = [ "multi-user.target" ]; - serviceConfig.ExecStart = "${pkgs.i2pd-exporter}/bin/i2pdexporter"; - }; - }; + networking.firewall = + mkIf cfg.openFirewall { allowedTCPPorts = [ cfg.port ] ++ optional (cfg.listenAddress != "127.0.0.1") cfg.port; }; + }; - } - ); + }; + devShells.default = + with nixpkgs; mkShell + { + LIBCLANG_PATH = "${pkgs.llvmPackages_17.libclang.lib}/lib"; + RUST_BACKTRACE = 1; + RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}"; + + packages = with pkgs; [ + rustc + cargo + rustfmt + rust-analyzer + clippy + rustup + pkg-config + openssl + ]; + }; + }; } +