Skip to content

Commit

Permalink
Add cross compiled packages
Browse files Browse the repository at this point in the history
  • Loading branch information
doronbehar committed Apr 12, 2023
1 parent 63d0ac0 commit 694b47c
Show file tree
Hide file tree
Showing 4 changed files with 156 additions and 352 deletions.
11 changes: 5 additions & 6 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

114 changes: 31 additions & 83 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
};
inputs.gomod2nix = {
# For static compilation I need: https://github.com/tweag/gomod2nix/pull/24
url = "github:doronbehar/gomod2nix/go-stdenv";
url = "github:nix-community/gomod2nix";
inputs.nixpkgs.follows = "nixpkgs";
inputs.utils.follows = "flake-utils";
};
Expand All @@ -33,101 +33,46 @@
pkgs = import nixpkgs {
inherit system;
overlays = [
gomod2nix.overlay
gomod2nix.overlays.default
];
};
inherit (gitignore.lib) gitignoreSource;
inherit (gitignore.lib) gitignoreFilterWith;
# 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";
# Create the buildGoApplication variants
inherit (pkgs) buildGoApplication;
buildGoApplicationStatic = buildGoApplication.override {
stdenv = pkgs.pkgsStatic.stdenv;
};
# Used also in the devShell
MAGIC_DB = "${pkgs.pkgsStatic.file}/share/misc/magic.mgc";
# arguments used in many derivation arguments calls
common-drv-args = {
pname = "pistol";
inherit version;
src = pkgs.lib.cleanSourceWith {
# Ignore many files that gitignoreSource doesn't ignore, see:
# https://github.com/hercules-ci/gitignore.nix/issues/9#issuecomment-635458762
filter = path: type:
! (builtins.any (r: (builtins.match r (builtins.baseNameOf path)) != null) [
# Nix files
"flake.nix"
"flake.lock"
"default.nix"
"shell.nix"
".envrc"
src = pkgs.lib.cleanSourceWith {
filter = gitignoreFilterWith {
basePath = ./.;
extraRules = ''
flake*
*.nix
./azure-pipelines.yml
.envrc
# Evaluated but not used for the build itself
"gomod2nix.toml"
"VERSION"
VERSION
"bump-version.sh"
# CI files
"renovate.json5"
renovate.json5
# Git files
".gitignore"
".git"
])
;
src = gitignoreSource ./.;
.gitignore
'';
};
buildFlagsArray = ''
-ldflags=
-X main.Version=${version}
'';
modules = ./gomod2nix.toml;
inherit (pkgs.pistol)
nativeBuildInputs
subPackages
postBuild
meta
;
CGO_ENABLED = 1;
src = ./.;
};
common-static-drv-args = (common-drv-args // {
nativeBuildInputs = common-drv-args.nativeBuildInputs ++ [
pkgs.removeReferencesTo
];
# From some reason even though zlib is static we need this, but it
# doesn't create a real reference to zlib.
NIX_LDFLAGS = "-lz";
preBuild = ''
cp ${MAGIC_DB} ./cmd/pistol/magic.mgc
'';
buildFlags = ''
-tags EMBED_MAGIC_DB
'';
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
'';
});
pistol = buildGoApplication (common-drv-args // {
inherit (pkgs.pistol) buildInputs;
});
pistol-static = buildGoApplicationStatic (common-static-drv-args // {
buildInputs = [
pkgs.pkgsStatic.file
pkgs.pkgsStatic.zlib
];
postFixup = common-static-drv-args.postFixup + ''
remove-references-to -t ${pkgs.pkgsStatic.file} $out/bin/pistol
'';
});
in rec {
pkgArgs = {
inherit version src;
};
pistol = pkgs.callPackage ./pkg.nix pkgArgs;
pistol-static = pkgs.pkgsStatic.callPackage ./pkg.nix pkgArgs;
pistol-static-aarch64 = pkgs.pkgsCross.aarch64-multiplatform-musl.pkgsStatic.callPackage ./pkg.nix pkgArgs;
pistol-static-armv7l = pkgs.pkgsCross.armv7l-hf-multiplatform.pkgsStatic.callPackage ./pkg.nix pkgArgs;
pistol-static-arm = pkgs.pkgsCross.arm-embedded.pkgsStatic.callPackage ./pkg.nix pkgArgs;
in {
devShell = pkgs.mkShell {
inherit (pistol) buildInputs;
nativeBuildInputs = pistol.nativeBuildInputs ++ [
nativeBuildInputs = [
pkgs.file
# For make check
pkgs.elinks
pkgs.gomod2nix
Expand All @@ -138,14 +83,17 @@
inherit
pistol
pistol-static
pistol-static-aarch64
pistol-static-armv7l
pistol-static-arm
;
};
defaultPackage = pistol;
apps.pistol = {
type = "app";
program = "${pistol}/bin/pistol";
};
defaultApp = apps.pistol;
defaultApp = self.apps.${system}.pistol;
}
);
}
Loading

0 comments on commit 694b47c

Please sign in to comment.