refactor: simplify flake.nix structure and improve go binary derivation

This commit is contained in:
Karol Broda
2025-12-21 01:57:32 +01:00
parent 04aa42a9c9
commit a93e682aa2
2 changed files with 77 additions and 95 deletions

18
flake.lock generated
View File

@@ -18,23 +18,7 @@
}, },
"root": { "root": {
"inputs": { "inputs": {
"nixpkgs": "nixpkgs", "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"
} }
} }
}, },

112
flake.nix
View File

@@ -1,107 +1,105 @@
{ {
description = "snitch - a friendlier ss/netstat for humans"; description = "snitch - a friendlier ss/netstat for humans";
inputs = { inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
nixpkgs.url = "github:NixOS/nixpkgs/nixos-25.05";
systems.url = "github:nix-systems/default";
};
outputs = { self, nixpkgs, systems }: outputs = { self, nixpkgs }:
let let
supportedSystems = import systems; systems = [ "x86_64-linux" "aarch64-linux" "x86_64-darwin" "aarch64-darwin" ];
forAllSystems = f: nixpkgs.lib.genAttrs supportedSystems (system: f system); eachSystem = nixpkgs.lib.genAttrs systems;
# go 1.25 overlay (required until nixpkgs has it) # go 1.25 binary derivation (required until nixpkgs ships it)
goOverlay = final: prev: mkGo125 = pkgs:
let let
version = "1.25.0"; version = "1.25.0";
platformInfo = { platform = {
"x86_64-linux" = { suffix = "linux-amd64"; sri = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; }; "x86_64-linux" = { suffix = "linux-amd64"; hash = "sha256-KFKvDLIKExObNEiZLmm4aOUO0Pih5ZQO4d6eGaEjthM="; GOOS = "linux"; GOARCH = "amd64"; };
"aarch64-linux" = { suffix = "linux-arm64"; sri = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; }; "aarch64-linux" = { suffix = "linux-arm64"; hash = "sha256-Bd511plKJ4NpmBXuVTvVqTJ9i3mZHeNuOLZoYngvVK4="; GOOS = "linux"; GOARCH = "arm64"; };
"x86_64-darwin" = { suffix = "darwin-amd64"; sri = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; }; "x86_64-darwin" = { suffix = "darwin-amd64"; hash = "sha256-W9YOgjA3BiwjB8cegRGAmGURZxTW9rQQWXz1B139gO8="; GOOS = "darwin"; GOARCH = "amd64"; };
"aarch64-darwin" = { suffix = "darwin-arm64"; sri = "sha256-VEkyhEFW2Bcveij3fyrJwVojBGaYtiQ/YzsKCwDAdJw="; }; "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}");
hostSystem = prev.stdenv.hostPlatform.system;
chosen = platformInfo.${hostSystem} or (throw "unsupported system: ${hostSystem}");
in in
{ pkgs.stdenv.mkDerivation {
go_1_25 = prev.stdenvNoCC.mkDerivation {
pname = "go"; pname = "go";
inherit version; inherit version;
src = prev.fetchurl { src = pkgs.fetchurl {
url = "https://go.dev/dl/go${version}.${chosen.suffix}.tar.gz"; url = "https://go.dev/dl/go${version}.${platform.suffix}.tar.gz";
hash = chosen.sri; inherit (platform) hash;
}; };
dontBuild = true; 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; dontPatchELF = true;
dontStrip = 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;
}; };
}; };
in
{
overlays.default = final: prev: {
snitch = final.callPackage ./nix/package.nix { };
};
packages = forAllSystems (system: pkgsFor = system: import nixpkgs { inherit system; };
let
pkgs = import nixpkgs { mkSnitch = pkgs:
inherit system;
overlays = [ goOverlay ];
};
in
let let
version = self.shortRev or self.dirtyShortRev or "dev"; version = self.shortRev or self.dirtyShortRev or "dev";
go = mkGo125 pkgs;
buildGoModule = pkgs.buildGoModule.override { inherit go; };
in in
{ buildGoModule {
default = pkgs.buildGoModule {
pname = "snitch"; pname = "snitch";
inherit version; inherit version;
src = self; src = self;
vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk="; vendorHash = "sha256-fX3wOqeOgjH7AuWGxPQxJ+wbhp240CW8tiF4rVUUDzk=";
env.CGO_ENABLED = 0; env.CGO_ENABLED = "0";
env.GOTOOLCHAIN = "local";
ldflags = [ ldflags = [
"-s" "-w" "-s"
"-w"
"-X snitch/cmd.Version=${version}" "-X snitch/cmd.Version=${version}"
"-X snitch/cmd.Commit=${version}" "-X snitch/cmd.Commit=${version}"
"-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}" "-X snitch/cmd.Date=${self.lastModifiedDate or "unknown"}"
]; ];
meta = with pkgs.lib; { meta = {
description = "a friendlier ss/netstat for humans"; description = "a friendlier ss/netstat for humans";
homepage = "https://github.com/karol-broda/snitch"; homepage = "https://github.com/karol-broda/snitch";
license = licenses.mit; license = pkgs.lib.licenses.mit;
platforms = platforms.linux; platforms = pkgs.lib.platforms.linux;
mainProgram = "snitch"; mainProgram = "snitch";
}; };
}; };
in
{
packages = eachSystem (system:
let pkgs = pkgsFor system; in
{
default = mkSnitch pkgs;
snitch = mkSnitch pkgs;
} }
); );
devShells = forAllSystems (system: devShells = eachSystem (system:
let let
pkgs = import nixpkgs { pkgs = pkgsFor system;
inherit system; go = mkGo125 pkgs;
overlays = [ goOverlay ];
};
in in
{ {
default = pkgs.mkShell { default = pkgs.mkShell {
packages = [ pkgs.go_1_25 pkgs.git pkgs.vhs ]; packages = [ go pkgs.git pkgs.vhs ];
GOTOOLCHAIN = "local"; env.GOTOOLCHAIN = "local";
shellHook = '' shellHook = ''
echo "go toolchain: $(go version)" echo "go toolchain: $(go version)"
''; '';
}; };
} }
); );
overlays.default = final: _prev: {
snitch = mkSnitch final;
};
}; };
} }