-
Notifications
You must be signed in to change notification settings - Fork 5
/
flake.nix
93 lines (80 loc) · 3.36 KB
/
flake.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
{
description = "A simple Go package";
inputs.nixpkgs.url = "github:NixOS/nixpkgs/nixos-24.05";
outputs = { self, nixpkgs }:
let
version = "0.2.1";
# System types to support.
supportedSystems = [ "x86_64-linux" "x86_64-darwin" "aarch64-linux" "aarch64-darwin" ];
# Helper function to generate an attrset '{ x86_64-linux = f "x86_64-linux"; ... }'.
forAllSystems = nixpkgs.lib.genAttrs supportedSystems;
# Nixpkgs instantiated for supported system types.
nixpkgsFor = forAllSystems (system: import nixpkgs { inherit system; });
dependenciesFor = pkgs : with pkgs; []
++ (lib.optionals stdenv.isLinux [
# For Linux clipboard support.
xorg.libX11.dev
])
++ (lib.optionals stdenv.isDarwin [
# For macOS clipboard support.
darwin.apple_sdk.frameworks.Cocoa
]);
in
{
# Provide some binary packages for selected system types.
packages = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
pname = "tsui";
linuxInterpreters = {
x86_64 = "/lib64/ld-linux-x86-64.so.2";
aarch64 = "/lib/ld-linux-aarch64.so.1";
};
linuxInterpreter = linuxInterpreters.${pkgs.stdenv.hostPlatform.parsed.cpu.name};
in
{
tsui = pkgs.buildGoModule {
inherit pname;
inherit version;
src = ./.;
# Possibly works around sporadic "signal: illegal instruction" error when
# cross-compiling with macOS Rosetta.
preBuild = if pkgs.stdenv.isLinux && pkgs.stdenv.isx86_64 then ''
export GODEBUG=asyncpreemptoff=1
'' else null;
# Inject the version info in the binary.
ldflags = [
"-X main.Version=${version}"
];
# This hash locks the dependencies of this package. It is
# necessary because of how Go requires network access to resolve
# VCS. See https://www.tweag.io/blog/2021-03-04-gomod2nix/ for
# details. Normally one can build with a fake hash and rely on native Go
# mechanisms to tell you what the hash should be or determine what
# it should be "out-of-band" with other tooling (eg. gomod2nix).
# Remember to bump this hash when your dependencies change.
vendorHash = "sha256-FIbkPE5KQ4w7Tc7kISQ7ZYFZAoMNGiVlFWzt8BPCf+A=";
buildInputs = dependenciesFor pkgs;
# Un-Nix the build so it can dlopen() X11 outside of Nix environments.
preFixup = if pkgs.stdenv.isLinux then ''
patchelf --remove-rpath --set-interpreter ${linuxInterpreter} $out/bin/${pname}
'' else null;
};
});
# Add dependencies that are only needed for development
devShells = forAllSystems (system:
let
pkgs = nixpkgsFor.${system};
in
{
default = pkgs.mkShell {
buildInputs = with pkgs; [ go gopls gotools go-tools ];
nativeBuildInputs = dependenciesFor pkgs;
};
});
# The default package for 'nix build'. This makes sense if the
# flake provides only one package or there is a clear "main"
# package.
defaultPackage = forAllSystems (system: self.packages.${system}.tsui);
};
}