From 1cabbce9f36424c59911856d8b18cd339e6f0970 Mon Sep 17 00:00:00 2001 From: Doron Behar Date: Wed, 20 Oct 2021 17:41:10 +0300 Subject: [PATCH] Add pistol-static compilation --- flake.lock | 11 ++++++----- flake.nix | 45 +++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/flake.lock b/flake.lock index ce279c4..2c13907 100644 --- a/flake.lock +++ b/flake.lock @@ -61,15 +61,16 @@ ] }, "locked": { - "lastModified": 1627572165, - "narHash": "sha256-MFpwnkvQpauj799b4QTBJQFEddbD02+Ln5k92QyHOSk=", - "owner": "tweag", + "lastModified": 1638001840, + "narHash": "sha256-Qknxy+YjVA39v/LwiUNsAcMAg9Df8aYAuqkH+EiAHRA=", + "owner": "doronbehar", "repo": "gomod2nix", - "rev": "67f22dd738d092c6ba88e420350ada0ed4992ae8", + "rev": "242884158539d4cc0bdc74ab718c499bad785a14", "type": "github" }, "original": { - "owner": "tweag", + "owner": "doronbehar", + "ref": "go-stdenv", "repo": "gomod2nix", "type": "github" } diff --git a/flake.nix b/flake.nix index f830c14..096a11e 100644 --- a/flake.nix +++ b/flake.nix @@ -11,7 +11,8 @@ flake = false; }; inputs.gomod2nix = { - url = "github:tweag/gomod2nix"; + # For static compilation I need: https://github.com/tweag/gomod2nix/pull/24 + url = "github:doronbehar/gomod2nix/go-stdenv"; inputs.nixpkgs.follows = "nixpkgs"; inputs.utils.follows = "flake-utils"; }; @@ -39,6 +40,9 @@ # https://discourse.nixos.org/t/passing-git-commit-hash-and-tag-to-build-with-flakes/11355/2 version_rev = if (self ? rev) then (builtins.substring 0 8 self.rev) else "dirty"; version = "${pkgs.lib.fileContents ./VERSION}-${version_rev}-flake"; + buildGoApplicationStatic = pkgs.buildGoApplication.override { + stdenv = pkgs.pkgsStatic.stdenv; + }; pistol = pkgs.buildGoApplication { pname = "pistol"; inherit version; @@ -79,6 +83,38 @@ ; CGO_ENABLED = 1; }; + pistol-static = buildGoApplicationStatic { + inherit (pistol) + pname + version + src + subPackages + postBuild + CGO_ENABLED + modules + meta + ; + nativeBuildInputs = pistol.nativeBuildInputs ++ [ + pkgs.removeReferencesTo + ]; + buildInputs = [ + pkgs.pkgsStatic.file + pkgs.pkgsStatic.zlib + ]; + # From some reason even though zlib is static we need this, but it + # doesn't create a real reference to zlib. + NIX_LDFLAGS = "-lz"; + postFixup = '' + # Remove unnecessary references to zlib. + rm -r $out/nix-support + # Remove more unnecessary references which I don't know the source of + # which. I guess they are due to features of some go modules I don't + # use. + remove-references-to -t ${pkgs.mailcap} $out/bin/pistol + remove-references-to -t ${pkgs.iana-etc} $out/bin/pistol + remove-references-to -t ${pkgs.tzdata} $out/bin/pistol + ''; + }; in rec { devShell = pkgs.mkShell { inherit (pistol) buildInputs; @@ -88,7 +124,12 @@ pkgs.gomod2nix ]; }; - packages.pistol = pistol; + packages = { + inherit + pistol + pistol-static + ; + }; defaultPackage = pistol; apps.pistol = { type = "app";