From a93e682aa20aa81bd05ceea7524c6ef6a67fe3db Mon Sep 17 00:00:00 2001 From: Karol Broda Date: Sun, 21 Dec 2025 01:57:32 +0100 Subject: [PATCH] refactor: simplify flake.nix structure and improve go binary derivation --- flake.lock | 18 +------ flake.nix | 154 ++++++++++++++++++++++++++--------------------------- 2 files changed, 77 insertions(+), 95 deletions(-) diff --git a/flake.lock b/flake.lock index bb25822..dbb6bec 100644 --- a/flake.lock +++ b/flake.lock @@ -18,23 +18,7 @@ }, "root": { "inputs": { - "nixpkgs": "nixpkgs", - "systems": "systems" - } - }, - "systems": { - "locked": { - "lastModified": 1681028828, - "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", - "owner": "nix-systems", - "repo": "default", - "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", - "type": "github" - }, - "original": { - "owner": "nix-systems", - "repo": "default", - "type": "github" + "nixpkgs": "nixpkgs" } } }, diff --git a/flake.nix b/flake.nix index b308624..92f8b0f 100644 --- a/flake.nix +++ b/flake.nix @@ -1,107 +1,105 @@ { description = "snitch - a friendlier ss/netstat for humans"; - inputs = { - nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; - systems.url = "github:nix-systems/default"; - }; + inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05"; - outputs = { self, nixpkgs, systems }: + outputs = { self, nixpkgs }: let - supportedSystems = import systems; - forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system); + systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ]; + eachSystem = nixpkgs.lib.genAttrs systems; - # go 1.25 overlay (required until nixpkgs has it) - goOverlay = final: prev: + # go 1.25 binary derivation (required until nixpkgs ships it) + mkGo125 = pkgs: let version = "1.25.0"; - platformInfo = { - "x86_64-linux" = { suffix = "linux-amd64"; sri = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; }; - "aarch64-linux" = { suffix = "linux-arm64"; sri = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; }; - "x86_64-darwin" = { suffix = "darwin-amd64"; sri = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; }; - "aarch64-darwin" = { suffix = "darwin-arm64"; sri = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; }; - }; - hostSystem = prev.stdenv.hostPlatform.system; - chosen = platformInfo.${hostSystem} or (throw "unsupported system: ${hostSystem}"); + platform = { + "x86_64-linux" = { suffix = "linux-amd64"; hash = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; GOOS = "linux"; GOARCH = "amd64"; }; + "aarch64-linux" = { suffix = "linux-arm64"; hash = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; GOOS = "linux"; GOARCH = "arm64"; }; + "x86_64-darwin" = { suffix = "darwin-amd64"; hash = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; GOOS = "darwin"; GOARCH = "amd64"; }; + "aarch64-darwin" = { suffix = "darwin-arm64"; hash = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; GOOS = "darwin"; GOARCH = "arm64"; }; + }.${pkgs.stdenv.hostPlatform.system} or (throw "unsupported system: ${pkgs.stdenv.hostPlatform.system}"); in - { - go_1_25 = prev.stdenvNoCC.mkDerivation { - pname = "go"; - inherit version; - src = prev.fetchurl { - url = "https://go.dev/dl/go${version}.${chosen.suffix}.tar.gz"; - hash = chosen.sri; - }; - dontBuild = true; - installPhase = '' - runHook preInstall - mkdir -p "$out"/{bin,share} - tar -C "$TMPDIR" -xzf "$src" - cp -a "$TMPDIR/go" "$out/share/go" - ln -s "$out/share/go/bin/go" "$out/bin/go" - ln -s "$out/share/go/bin/gofmt" "$out/bin/gofmt" - runHook postInstall - ''; - dontPatchELF = true; - dontStrip = true; + pkgs.stdenv.mkDerivation { + pname = "go"; + inherit version; + src = pkgs.fetchurl { + url = "https://go.dev/dl/go${version}.${platform.suffix}.tar.gz"; + inherit (platform) hash; + }; + dontBuild = true; + dontPatchELF = true; + dontStrip = true; + installPhase = '' + runHook preInstall + mkdir -p $out/{bin,share/go} + tar -xzf $src --strip-components=1 -C $out/share/go + ln -s $out/share/go/bin/go $out/bin/go + ln -s $out/share/go/bin/gofmt $out/bin/gofmt + runHook postInstall + ''; + passthru = { + inherit (platform) GOOS GOARCH; + }; + }; + + pkgsFor = system: import nixpkgs { inherit system; }; + + mkSnitch = pkgs: + let + version = self.shortRev or self.dirtyShortRev or "dev"; + go = mkGo125 pkgs; + buildGoModule = pkgs.buildGoModule.override { inherit go; }; + in + buildGoModule { + pname = "snitch"; + inherit version; + src = self; + vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk="; + env.CGO_ENABLED = "0"; + env.GOTOOLCHAIN = "local"; + ldflags = [ + "-s" + "-w" + "-X snitch/cmd.Version=${version}" + "-X snitch/cmd.Commit=${version}" + "-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}" + ]; + meta = { + description = "a friendlier ss/netstat for humans"; + homepage = "https://github.com/karol-broda/snitch"; + license = pkgs.lib.licenses.mit; + platforms = pkgs.lib.platforms.linux; + mainProgram = "snitch"; }; }; in { - overlays.default = final: prev: { - snitch = final.callPackage ./nix/package.nix { }; - }; - - packages = forAllSystems (system: - let - pkgs = import nixpkgs { - inherit system; - overlays = [ goOverlay ]; - }; - in - let - version = self.shortRev or self.dirtyShortRev or "dev"; - in + packages = eachSystem (system: + let pkgs = pkgsFor system; in { - default = pkgs.buildGoModule { - pname = "snitch"; - inherit version; - src = self; - vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk="; - env.CGO_ENABLED = 0; - ldflags = [ - "-s" "-w" - "-X snitch/cmd.Version=${version}" - "-X snitch/cmd.Commit=${version}" - "-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}" - ]; - meta = with pkgs.lib; { - description = "a friendlier ss/netstat for humans"; - homepage = "https://github.com/karol-broda/snitch"; - license = licenses.mit; - platforms = platforms.linux; - mainProgram = "snitch"; - }; - }; + default = mkSnitch pkgs; + snitch = mkSnitch pkgs; } ); - devShells = forAllSystems (system: + devShells = eachSystem (system: let - pkgs = import nixpkgs { - inherit system; - overlays = [ goOverlay ]; - }; + pkgs = pkgsFor system; + go = mkGo125 pkgs; in { default = pkgs.mkShell { - packages = [ pkgs.go_1_25 pkgs.git pkgs.vhs ]; - GOTOOLCHAIN = "local"; + packages = [ go pkgs.git pkgs.vhs ]; + env.GOTOOLCHAIN = "local"; shellHook = '' echo "go toolchain: $(go version)" ''; }; } ); + + overlays.default = final: _prev: { + snitch = mkSnitch final; + }; }; }