From adf7c84269a517266052d05cbc108d6e5d212f5a Mon Sep 17 00:00:00 2001 From: tux Date: Tue, 8 Sep 2026 04:18:48 +0530 Subject: [PATCH] refactor(nix): migrate flake to flake-parts --- flake.lock | 57 ++++++++++++++++++++++++++++- flake.nix | 66 +++++++++++++++++++--------------- module.nix => nix/module.nix | 26 ++++++-------- package.nix => nix/package.nix | 7 ++-- nix/shell.nix | 4 +++ shell.nix | 13 ------- 6 files changed, 110 insertions(+), 63 deletions(-) rename module.nix => nix/module.nix (84%) rename package.nix => nix/package.nix (85%) create mode 100644 nix/shell.nix delete mode 100644 shell.nix diff --git a/flake.lock b/flake.lock index e604777..d2da546 100644 --- a/flake.lock +++ b/flake.lock @@ -1,5 +1,23 @@ { "nodes": { + "flake-parts": { + "inputs": { + "nixpkgs-lib": "nixpkgs-lib" + }, + "locked": { + "lastModified": 1788450739, + "narHash": "sha256-glZLQlzIn1fXH6PazR2iUmTo7kzzyYSshrWhLS9TqCU=", + "owner": "hercules-ci", + "repo": "flake-parts", + "rev": "31729ca8cbdb4fa927b34e5f4353e6a83f39e993", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "flake-parts", + "type": "github" + } + }, "nixpkgs": { "locked": { "lastModified": 1740547748, @@ -16,9 +34,46 @@ "type": "github" } }, + "nixpkgs-lib": { + "locked": { + "lastModified": 1788057806, + "narHash": "sha256-DTQSMxzDWmT0zhguthvegnVkn7CFqGCv4IHCzk5ZUpM=", + "owner": "nix-community", + "repo": "nixpkgs.lib", + "rev": "596e2e3940e09b2abbeb03f75fa1828c57fcd72c", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "nixpkgs.lib", + "type": "github" + } + }, "root": { "inputs": { - "nixpkgs": "nixpkgs" + "flake-parts": "flake-parts", + "nixpkgs": "nixpkgs", + "treefmt-nix": "treefmt-nix" + } + }, + "treefmt-nix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1786901030, + "narHash": "sha256-WSFCsDSE5ffgD2MqzkM2CYjeFiKhRF/dJUN8uedb6YE=", + "owner": "numtide", + "repo": "treefmt-nix", + "rev": "27b3b12a8e6375f28ebe122f07d230ca5459bbfa", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "treefmt-nix", + "type": "github" } } }, diff --git a/flake.nix b/flake.nix index c3159f7..b748913 100644 --- a/flake.nix +++ b/flake.nix @@ -1,33 +1,43 @@ { - description = "Simple tunneler in Go that exposes local ports to the internet"; - inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + description = + "Simple tunneler in Go that exposes local ports to the internet"; - outputs = - { - self, - nixpkgs, - }: - let - systems = [ - "x86_64-linux" - "aarch64-linux" - "x86_64-darwin" - "aarch64-darwin" - ]; + inputs = { + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + flake-parts.url = "github:hercules-ci/flake-parts"; - forAllSystems = - function: nixpkgs.lib.genAttrs systems (system: function nixpkgs.legacyPackages.${system}); - in - { - packages = forAllSystems (pkgs: rec { - default = trok; - trok = pkgs.callPackage ./package.nix { }; - }); - - nixosModules.default = ./module.nix; - - devShells = forAllSystems (pkgs: { - default = pkgs.callPackage ./shell.nix { }; - }); + treefmt-nix = { + url = "github:numtide/treefmt-nix"; + inputs.nixpkgs.follows = "nixpkgs"; }; + }; + + outputs = inputs: + inputs.flake-parts.lib.mkFlake { inherit inputs; } { + imports = [ inputs.treefmt-nix.flakeModule ]; + + systems = + [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + + perSystem = { config, pkgs, ... }: { + treefmt = { + projectRootFile = "flake.nix"; + + programs = { + nixfmt.enable = true; + gofumpt.enable = true; + }; + }; + + packages = rec { + default = tfolio; + tfolio = pkgs.callPackage ./nix/package.nix { }; + }; + + devShells.default = pkgs.callPackage ./nix/shell.nix { }; + }; + + flake.nixosModules.default = ./nix/module.nix; + }; + } diff --git a/module.nix b/nix/module.nix similarity index 84% rename from module.nix rename to nix/module.nix index 74a284a..01fae48 100644 --- a/module.nix +++ b/nix/module.nix @@ -1,11 +1,6 @@ -{ - config, - lib, - pkgs, - ... -}: -with lib; let - cfg = config.services.trok; +{ config, lib, pkgs, ... }: +with lib; +let cfg = config.services.trok; in { options.services.trok = { enable = mkEnableOption "Enable trok"; @@ -38,19 +33,20 @@ in { }; config = mkIf cfg.enable { - networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [cfg.port]; + networking.firewall.allowedTCPPorts = mkIf cfg.openFirewall [ cfg.port ]; systemd.services = { trok = { description = "trok server"; - after = ["network.target"]; - wantedBy = ["multi-user.target"]; + after = [ "network.target" ]; + wantedBy = [ "multi-user.target" ]; serviceConfig = { Type = "simple"; User = "trok"; Group = "trok"; - ExecStart = "${getExe pkgs.trok} server -a ${cfg.host}:${toString cfg.port}"; + ExecStart = + "${getExe pkgs.trok} server -a ${cfg.host}:${toString cfg.port}"; Restart = "always"; LockPersonality = true; @@ -73,7 +69,7 @@ in { RestrictRealtime = true; RestrictSUIDSGID = true; SystemCallArchitectures = "native"; - SystemCallFilter = ["@system-service"]; + SystemCallFilter = [ "@system-service" ]; UMask = "0077"; }; }; @@ -89,8 +85,6 @@ in { }; }; - users.groups = mkIf (cfg.group == "trok") { - ${cfg.group} = {}; - }; + users.groups = mkIf (cfg.group == "trok") { ${cfg.group} = { }; }; }; } diff --git a/package.nix b/nix/package.nix similarity index 85% rename from package.nix rename to nix/package.nix index cae9be6..545131c 100644 --- a/package.nix +++ b/nix/package.nix @@ -1,12 +1,9 @@ -{ - lib, - buildGoModule, -}: +{ lib, buildGoModule }: buildGoModule { pname = "trok"; version = "0.2.0"; - src = ./.; + src = ../.; vendorHash = "sha256-KwcLxkW3pbzujjc6JOZRwATlAA/qndf4FWpkJANv2z8="; meta = { diff --git a/nix/shell.nix b/nix/shell.nix new file mode 100644 index 0000000..50f8aec --- /dev/null +++ b/nix/shell.nix @@ -0,0 +1,4 @@ +{ callPackage, go, }: +let mainPkg = callPackage ./package.nix { }; +in mainPkg.overrideAttrs +(oa: { nativeBuildInputs = [ go ] ++ (oa.nativeBuildInputs or [ ]); }) diff --git a/shell.nix b/shell.nix deleted file mode 100644 index a3d3f37..0000000 --- a/shell.nix +++ /dev/null @@ -1,13 +0,0 @@ -{ - callPackage, - go, -}: -let - mainPkg = callPackage ./package.nix { }; -in -mainPkg.overrideAttrs (oa: { - nativeBuildInputs = [ - go - ] - ++ (oa.nativeBuildInputs or [ ]); -})