From e7fe37458bd247c6b504911144f445f8bc717730 Mon Sep 17 00:00:00 2001 From: Jeremy Baxter Date: Sat, 26 Oct 2024 20:11:08 +1300 Subject: [PATCH] flake.nix: init --- flake.lock | 27 +++++++++++++++ flake.nix | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 flake.lock create mode 100644 flake.nix diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..4249bfa --- /dev/null +++ b/flake.lock @@ -0,0 +1,27 @@ +{ + "nodes": { + "nixpkgs": { + "locked": { + "lastModified": 1729665710, + "narHash": "sha256-AlcmCXJZPIlO5dmFzV3V2XF6x/OpNWUV8Y/FMPGd8Z4=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "2768c7d042a37de65bb1b5b3268fc987e534c49d", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "nixpkgs": "nixpkgs" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..0e9bcbc --- /dev/null +++ b/flake.nix @@ -0,0 +1,97 @@ +{ + description = "filesystem monitor"; + + inputs = { + nixpkgs.url = github:NixOS/nixpkgs/nixos-unstable; + }; + + outputs = { self, nixpkgs }: + let + plats = nixpkgs.lib.platforms.unix; + forAllSystems = fn: + nixpkgs.lib.genAttrs plats (system: + fn (import nixpkgs { + inherit system; + }) + ); + in + { + packages = forAllSystems (pkgs: { + default = pkgs.stdenv.mkDerivation (finalAttrs: { + name = "bluetop"; + src = ./.; + + # overridable attributes + fileSystems = ["/"]; + indexPage = null; + + nativeBuildInputs = with pkgs; [ ldc ]; + + postPatch = + let + mounts = toString (pkgs.lib.forEach finalAttrs.fileSystems (f: ''"${f}",'')); + in + '' + ${if finalAttrs.indexPage != null + then "install -Dm644 ${finalAttrs.indexPage} static/index.html" + else ""} + sed -E 's:mountpoints = \[.+\];$:mountpoints = \[${mounts}\];:' buildconf.d | tee buildconf.d + ''; + + dontAddPrefix = true; + enableParallelBuilding = true; + installFlags = [ + "DESTDIR=$(out)" + "PREFIX=/" + ]; + + meta.mainProgram = "bluetopd"; + }); + }); + nixosModule = { config, lib, pkgs, ... }: + with lib; + let + cfg = config.services.bluetop; + in + { + options = { + services.bluetop = { + enable = mkEnableOption "bluetop, a status page displaying storage statistics"; + package = mkPackageOption pkgs "bluetop" { }; + openFirewall = mkOption { + type = types.bool; + default = false; + description = "Whether to open the bluetop port to the firewall."; + }; + port = mkOption { + type = types.port; + default = 8089; + description = "Port for bluetop to bind to."; + }; + }; + }; + + config = mkIf cfg.enable { + users.groups.bluetop = {}; + users.users.bluetop = { + description = "bluetop service user"; + group = "bluetop"; + isSystemUser = true; + }; + + systemd.services.bluetopd = { + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; + script = '' + set -eu + ${getExe cfg.package} --port ${toString cfg.port} + ''; + }; + + networking.firewall = mkIf cfg.openFirewall { + allowedTCPPorts = [ cfg.port ]; + }; + }; + }; + }; +}