-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathflake.nix
More file actions
209 lines (188 loc) · 6.96 KB
/
Copy pathflake.nix
File metadata and controls
209 lines (188 loc) · 6.96 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
{
nixConfig = {
extra-substituters = [
"https://cache.rustlang-es.org/main"
];
extra-trusted-public-keys = [
"main:NnVmqBjdfyyL4tGgoTw17lUMDgulJ75+67pOsJupnS4="
];
};
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
rust-overlay = {
url = "github:oxalica/rust-overlay";
inputs.nixpkgs.follows = "nixpkgs";
};
};
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
flake-utils.lib.eachDefaultSystem (baseSystem:
let
cargoManifest = builtins.fromTOML (builtins.readFile ./Cargo.toml);
architectures = import ./architectures.nix;
overlays = [ (import rust-overlay) ];
pkgs = import nixpkgs {
system = baseSystem;
inherit overlays;
};
lib = pkgs.lib;
uniqueBy = filter: list:
let
aux = seen: remaining:
if remaining == [] then []
else
let
item = builtins.head remaining;
rest = builtins.tail remaining;
in
if builtins.elem (filter item) seen then
aux seen rest
else
[item] ++ aux (seen ++ [(filter item)]) rest;
in aux [] list;
mkCrossPkgs = { arch, os }: let
cross = arch + "-" + os;
crossSystem = lib.systems.elaborate cross;
in import nixpkgs {
inherit overlays;
crossSystem = if cross != "x86_64-linux" then crossSystem else null;
localSystem = baseSystem;
};
mkDevShell = { arch, os, ... }: pkgs.mkShell {
packages = with pkgs; [ rustc cargo ];
shellHook = ''
echo "DevShell for grhooks ${arch}-${os}"
'';
};
mkPackage = { arch, os, target, ... }: let
crossPkgs = mkCrossPkgs { inherit arch os; };
in pkgs.rustPlatform.buildRustPackage {
pname = "grhooks";
version = "0.1.0";
src = ./.;
cargoLock.lockFile = ./Cargo.lock;
CARGO_BUILD_TARGET = target;
HOST_CC = lib.optionalString (os != "windows") "${pkgs.stdenv.cc.nativePrefix}cc";
TARGET_CC = lib.optionalString (os != "windows") "${crossPkgs.stdenv.cc.targetPrefix}cc";
buildInputs = with pkgs; [ ]
++ lib.optionals (os == "linux") [ stdenv.cc glibc ]
++ lib.optionals (os == "macos") [ clang darwin.apple_sdk.frameworks.CoreFoundation ];
};
containerPkg = variant: let
pkg = mkPackage variant;
dockerPlatform =
if variant.arch == "x86_64" then "amd64"
else if variant.arch == "aarch64" then "arm64"
else if variant.arch == "armv7l" then "arm"
else if variant.arch == "armv6l" then "arm"
else if variant.arch == "i686" then "386"
else throw "Unsupported arch: ${variant.arch}";
in pkgs.dockerTools.buildLayeredImage rec {
name = cargoManifest.package.name;
tag = cargoManifest.package.version;
created = "now";
architecture = dockerPlatform;
contents = [ pkg ];
config.Cmd = ["/bin/${name}"];
};
generatedMatrixJson = builtins.toJSON (lib.flatten (map ({ arch, os, formats, ... }:
map (format: {
arch = arch;
os = os;
format = format;
package = "${os}-${arch}";
bundler = if format == "zip" then ".#toZip"
else if format == "deb" then "github:NixOS/bundlers#toDEB"
else if format == "rpm" then "github:NixOS/bundlers#toRPM"
else ".#toTarball";
}) formats
) architectures));
archList = uniqueBy (i: i.arch) architectures;
generatedArchJson = builtins.toJSON archList;
in
{
devShells = lib.listToAttrs (map ({ arch, os, target, ... }: {
name = "${os}-${arch}";
value = mkDevShell { inherit arch os target; };
}) architectures) // {
# Default Devshell
default = mkDevShell {
arch = "x86_64";
os = "linux";
target = "x86_64-unknown-linux-gnu";
};
};
packages = (lib.listToAttrs (map ({ arch, os, target, ... }: {
name = "${os}-${arch}";
value = mkPackage { inherit arch os target; };
}) architectures)) // (pkgs.lib.listToAttrs (map ({arch, ...} @ args: {
name = "image-${arch}";
value = containerPkg args;
}) archList));
apps = {
help = {
type = "app";
program = toString (pkgs.writeScript "help" ''
#!/bin/sh
echo ""
echo "Welcome to Grhooks!"
echo ""
echo -e "\033[0;33mAvailable architectures:\033[0m"
${lib.concatMapStringsSep "\n" (arch: ''echo " - ${arch}"'') (lib.lists.unique (map ({ arch, ... }: arch) architectures))}
echo ""
echo -e "\033[0;35mAvailable OS:\033[0m"
${lib.concatMapStringsSep "\n" (os: ''echo " - ${os}"'') (lib.lists.unique (map ({ os, ... }: os) architectures))}
echo ""
echo -e "\033[0;32mTo build docker image, use:\033[0m"
echo " nix build .#image-<arch>"
echo -e "\033[0;32mTo build a specific variant, use:\033[0m"
echo " nix build .#<os>-<arch>"
echo ""
echo -e "\033[0;32mExample:\033[0m"
echo " nix build .#linux-x86_64"
'');
};
matrix = {
type = "app";
program = toString (pkgs.writeScript "generate-matrix" ''
#!/bin/sh
echo '${generatedMatrixJson}'
'');
};
archs = {
type = "app";
program = toString (pkgs.writeScript "generate-arch-list" ''
#!/bin/sh
echo '${generatedArchJson}'
'');
};
};
bundlers = let
compress = ext: name: drv: pkgs.stdenv.mkDerivation {
name = "${name}-${drv.name}";
buildInputs = with pkgs; [ouch];
unpackPhase = "true";
buildPhase = ''
export HOME=$PWD
mkdir -p ./nix/store/
mkdir -p ./bin
for item in "$(cat ${pkgs.referencesByPopularity drv})"
do
cp -r $item ./nix/store/
done
cp -r ${drv}/bin/* ./bin/
chmod -R a+rwx ./nix
chmod -R a+rwx ./bin
ouch c nix bin ${drv.name}.${ext}
'';
installPhase = ''
mkdir -p $out
cp -r *.${ext} $out
'';
};
in {
toTarball = compress "tar.xz" "tar-simple";
toZip = compress "zip" "zip-simple";
};
});
}