From 20029edd1c89e4d747d6a48fa2aff3880f485441 Mon Sep 17 00:00:00 2001 From: NotAShelf Date: Fri, 6 Mar 2026 22:44:41 +0300 Subject: [PATCH] nix: initiate NixOS module; add `ncro` to `packages` output Signed-off-by: NotAShelf Change-Id: I4948bbc2a53adbc64727a1dc935b750e6a6a6964 --- flake.nix | 9 +++++-- nix/module.nix | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 nix/module.nix diff --git a/flake.nix b/flake.nix index c8cddf2..3a1b1da 100644 --- a/flake.nix +++ b/flake.nix @@ -1,5 +1,4 @@ { - description = "Go Project Template"; inputs.nixpkgs.url = "github:NixOS/nixpkgs?ref=nixos-unstable"; outputs = { @@ -12,13 +11,19 @@ pkgsForEach = nixpkgs.legacyPackages; in { packages = forEachSystem (system: { - default = pkgsForEach.${system}.callPackage ./nix/package.nix {}; + ncro = pkgsForEach.${system}.callPackage ./nix/package.nix {}; + default = self.packages.${system}.ncro; }); devShells = forEachSystem (system: { default = pkgsForEach.${system}.callPackage ./nix/shell.nix {}; }); + nixosModules = { + ncro = import ./nix/module.nix self; + default = self.nixosModules.ncro; + }; + hydraJobs = self.packages; }; } diff --git a/nix/module.nix b/nix/module.nix new file mode 100644 index 0000000..3a02a37 --- /dev/null +++ b/nix/module.nix @@ -0,0 +1,67 @@ +self: { + config, + pkgs, + lib, + ... +}: let + inherit (lib.modules) mkIf; + inherit (lib.options) mkOption mkEnableOption mkPackageOption; + + format = pkgs.formats.yaml {}; + + cfg = config.services.ncro; + configFile = format.generate "ncro.yaml" cfg.settings; +in { + options.services.ncro = { + enable = mkEnableOption "ncro, the Nix cache route optimizer"; + + package = mkPackageOption self.packages.${pkgs.stdenv.hostPlatform.system} ["ncro"] {}; + + settings = mkOption { + type = format.type; + default = {}; + description = '' + ncro configuration as an attribute set. Keys and structure match the + YAML config file format; all defaults are handled by the ncro binary. + ''; + example = { + logging.level = "info"; + server = { + listen = ":8080"; + cache_priority = 20; + }; + + upstreams = [ + { + url = "https://cache.nixos.org"; + priority = 10; + } + { + url = "https://nix-community.cachix.org"; + priority = 20; + } + ]; + + cache = { + ttl = "2h"; + negative_ttl = "15m"; + }; + }; + }; + }; + + config = mkIf cfg.enable { + systemd.services.ncro = { + description = "Nix Cache Route Optimizer"; + wantedBy = ["multi-user.target"]; + after = ["network.target"]; + serviceConfig = { + ExecStart = "${lib.getExe cfg.package} -config ${configFile}"; + DynamicUser = true; + StateDirectory = "ncro"; + Restart = "on-failure"; + RestartSec = "5s"; + }; + }; + }; +}