From 144d5c45c636a5d189d3d0e328232edf3d988163 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jalil=20David=20Salam=C3=A9=20Messina?= Date: Mon, 30 Sep 2024 16:14:02 +0200 Subject: [PATCH] fix(fmt): use nixfmt-rfc-style --- default.nix | 55 ++++++++++++------------ flake.nix | 120 +++++++++++++++++++++++++++++----------------------- module.nix | 72 +++++++++++++++++-------------- run-cmd.nix | 4 +- 4 files changed, 139 insertions(+), 112 deletions(-) diff --git a/default.nix b/default.nix index a374ee4..a3b6c80 100644 --- a/default.nix +++ b/default.nix @@ -1,7 +1,8 @@ { lib, rustPlatform, -}: let +}: +let readToml = path: builtins.fromTOML (builtins.readFile path); cargoToml = readToml ./Cargo.toml; pname = cargoToml.package.name; @@ -11,32 +12,34 @@ name = "${pname}-source"; # Adapted from # no need to pull in crane for just this - filter = orig_path: type: let - path = toString orig_path; - base = baseNameOf path; - parentDir = baseNameOf (dirOf path); - matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [ - # Rust sources - ".rs" - # TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml) - ".toml" - ]; - isCargoLock = base == "Cargo.lock"; - # .cargo/config.toml is captured above - isOldStyleCargoConfig = parentDir == ".cargo" && base == "config"; - in + filter = + orig_path: type: + let + path = toString orig_path; + base = baseNameOf path; + parentDir = baseNameOf (dirOf path); + matchesSuffix = lib.any (suffix: lib.hasSuffix suffix base) [ + # Rust sources + ".rs" + # TOML files are often used to configure cargo based tools (e.g. .cargo/config.toml) + ".toml" + ]; + isCargoLock = base == "Cargo.lock"; + # .cargo/config.toml is captured above + isOldStyleCargoConfig = parentDir == ".cargo" && base == "config"; + in type == "directory" || matchesSuffix || isCargoLock || isOldStyleCargoConfig; }; in - rustPlatform.buildRustPackage { - inherit pname version src; - cargoLock.lockFile = ./Cargo.lock; - useNextest = true; +rustPlatform.buildRustPackage { + inherit pname version src; + cargoLock.lockFile = ./Cargo.lock; + useNextest = true; - meta = { - inherit description; - license = lib.licenses.mit; - homepage = "https://github.com/jalil-salame/webnsupdate"; - mainProgram = "webnsupdate"; - }; - } + meta = { + inherit description; + license = lib.licenses.mit; + homepage = "https://github.com/jalil-salame/webnsupdate"; + mainProgram = "webnsupdate"; + }; +} diff --git a/flake.nix b/flake.nix index 530439b..d78eb96 100644 --- a/flake.nix +++ b/flake.nix @@ -5,59 +5,75 @@ systems.url = "github:nix-systems/default"; }; - outputs = { - self, - nixpkgs, - systems, - }: let - forEachSupportedSystem = nixpkgs.lib.genAttrs (import systems); - in { - checks = forEachSupportedSystem (system: let - pkgs = nixpkgs.legacyPackages.${system}; - inherit (nixpkgs) lib; - in { - fmtRust = pkgs.callPackage ./run-cmd.nix { - src = self; - name = "fmt-rust"; - extraNativeBuildInputs = [pkgs.rustfmt]; - cmd = "${lib.getExe pkgs.cargo} fmt --all --check --verbose"; - }; - fmtNix = pkgs.callPackage ./run-cmd.nix { - src = self; - name = "fmt-nix"; - cmd = "${lib.getExe pkgs.alejandra} --check ."; - }; - lintNix = pkgs.callPackage ./run-cmd.nix { - src = self; - name = "lint-nix"; - cmd = "${lib.getExe pkgs.statix} check ."; - }; - }); - formatter = forEachSupportedSystem (system: nixpkgs.legacyPackages.${system}.alejandra); + outputs = + { + self, + nixpkgs, + systems, + }: + let + forEachSupportedSystem = nixpkgs.lib.genAttrs (import systems); + in + { + checks = forEachSupportedSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + inherit (nixpkgs) lib; + in + { + fmtRust = pkgs.callPackage ./run-cmd.nix { + src = self; + name = "fmt-rust"; + extraNativeBuildInputs = [ pkgs.rustfmt ]; + cmd = "${lib.getExe pkgs.cargo} fmt --all --check --verbose"; + }; + fmtNix = pkgs.callPackage ./run-cmd.nix { + src = self; + name = "fmt-nix"; + cmd = "${lib.getExe self.formatter.${system}} --check ."; + }; + lintNix = pkgs.callPackage ./run-cmd.nix { + src = self; + name = "lint-nix"; + cmd = "${lib.getExe pkgs.statix} check ."; + }; + } + ); + formatter = forEachSupportedSystem (system: nixpkgs.legacyPackages.${system}.nixfmt-rfc-style); - packages = forEachSupportedSystem (system: let - webnsupdate = nixpkgs.legacyPackages.${system}.callPackage ./default.nix {}; - in { - inherit webnsupdate; - default = webnsupdate; - }); + packages = forEachSupportedSystem ( + system: + let + webnsupdate = nixpkgs.legacyPackages.${system}.callPackage ./default.nix { }; + in + { + inherit webnsupdate; + default = webnsupdate; - overlays.default = final: prev: { - webnsupdate = final.callPackage ./default.nix {}; + } + ); + + overlays.default = final: prev: { + webnsupdate = final.callPackage ./default.nix { }; + }; + + nixosModules.default = ./module.nix; + + devShells = forEachSupportedSystem ( + system: + let + pkgs = nixpkgs.legacyPackages.${system}; + in + { + default = pkgs.mkShell { + packages = [ + pkgs.cargo-insta + pkgs.cargo-udeps + pkgs.mold + ]; + }; + } + ); }; - - nixosModules.default = ./module.nix; - - devShells = forEachSupportedSystem (system: let - pkgs = nixpkgs.legacyPackages.${system}; - in { - default = pkgs.mkShell { - packages = [ - pkgs.cargo-insta - pkgs.cargo-udeps - pkgs.mold - ]; - }; - }); - }; } diff --git a/module.nix b/module.nix index 2f3834d..203998f 100644 --- a/module.nix +++ b/module.nix @@ -3,13 +3,15 @@ pkgs, config, ... -}: let +}: +let cfg = config.services.webnsupdate; inherit (lib) mkOption mkEnableOption types; -in { +in +{ options.services.webnsupdate = mkOption { description = "An HTTP server for nsupdate."; - default = {}; + default = { }; type = types.submodule { options = { enable = mkEnableOption "webnsupdate"; @@ -18,8 +20,8 @@ in { Extra arguments to be passed to the webnsupdate server command. ''; type = types.listOf types.str; - default = []; - example = ["--ip-source"]; + default = [ ]; + example = [ "--ip-source" ]; }; bindIp = mkOption { description = '' @@ -102,47 +104,53 @@ in { }; }; - config = let - recordsFile = - if cfg.recordsFile != null - then cfg.recordsFile - else pkgs.writeText "webnsrecords" cfg.records; - args = lib.strings.escapeShellArgs ([ - "--records" - recordsFile - "--key-file" - cfg.keyFile - "--password-file" - cfg.passwordFile - "--address" - cfg.bindIp - "--port" - (builtins.toString cfg.bindPort) - "--ttl" - (builtins.toString cfg.ttl) - ] - ++ cfg.extraArgs); - cmd = "${lib.getExe pkgs.webnsupdate} ${args}"; - in + config = + let + recordsFile = + if cfg.recordsFile != null then cfg.recordsFile else pkgs.writeText "webnsrecords" cfg.records; + args = lib.strings.escapeShellArgs ( + [ + "--records" + recordsFile + "--key-file" + cfg.keyFile + "--password-file" + cfg.passwordFile + "--address" + cfg.bindIp + "--port" + (builtins.toString cfg.bindPort) + "--ttl" + (builtins.toString cfg.ttl) + ] + ++ cfg.extraArgs + ); + cmd = "${lib.getExe pkgs.webnsupdate} ${args}"; + in lib.mkIf cfg.enable { # warnings = # lib.optional (!config.services.bind.enable) "`webnsupdate` is expected to be used alongside `bind`. This is an unsopported configuration."; assertions = [ { - assertion = (cfg.records != null || cfg.recordsFile != null) && !(cfg.records != null && cfg.recordsFile != null); + assertion = + (cfg.records != null || cfg.recordsFile != null) + && !(cfg.records != null && cfg.recordsFile != null); message = "Exactly one of `services.webnsupdate.records` and `services.webnsupdate.recordsFile` must be set."; } ]; systemd.services.webnsupdate = { description = "Web interface for nsupdate."; - wantedBy = ["multi-user.target"]; - after = ["network.target" "bind.service"]; + wantedBy = [ "multi-user.target" ]; + after = [ + "network.target" + "bind.service" + ]; preStart = "${cmd} verify"; - path = [pkgs.dig]; + path = [ pkgs.dig ]; startLimitIntervalSec = 60; serviceConfig = { - ExecStart = [cmd]; + ExecStart = [ cmd ]; Type = "exec"; Restart = "on-failure"; RestartSec = "10s"; diff --git a/run-cmd.nix b/run-cmd.nix index e483e7b..02bbe9f 100644 --- a/run-cmd.nix +++ b/run-cmd.nix @@ -3,8 +3,8 @@ src, name, cmd, - extraBuildInputs ? [], - extraNativeBuildInputs ? [], + extraBuildInputs ? [ ], + extraNativeBuildInputs ? [ ], }: stdenvNoCC.mkDerivation { name = "${name}-src";