Skip to content

Commit 0e2135f

Browse files
committed
build: complete build and package with nix
1 parent d0f6c61 commit 0e2135f

File tree

1 file changed

+148
-18
lines changed

1 file changed

+148
-18
lines changed

flake.nix

Lines changed: 148 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{
2-
description = "Powered Hardware test tool written in Rust";
3-
42
inputs = {
53
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
64
flake-utils.url = "github:numtide/flake-utils";
@@ -10,24 +8,156 @@
108
};
119
};
1210

13-
outputs = {
14-
nixpkgs,
15-
flake-utils,
16-
rust-overlay,
17-
...
18-
}:
19-
flake-utils.lib.eachDefaultSystem (
20-
system: let
21-
overlays = [(import rust-overlay)];
11+
outputs = { nixpkgs, flake-utils, rust-overlay, ... }:
12+
flake-utils.lib.eachDefaultSystem (baseSystem:
13+
let
14+
overlays = [ (import rust-overlay) ];
2215
pkgs = import nixpkgs {
23-
inherit system overlays;
16+
system = baseSystem;
17+
inherit overlays;
18+
};
19+
lib = pkgs.lib;
20+
21+
mkCrossPkgs = { arch, os }: let
22+
cross = arch + "-" + os;
23+
crossSystem = lib.systems.elaborate cross;
24+
in import nixpkgs {
25+
inherit overlays;
26+
crossSystem = if cross != "x86_64-linux" then crossSystem else null;
27+
localSystem = baseSystem;
28+
};
29+
30+
architectures = [
31+
# Linux
32+
{ arch = "x86_64"; os = "linux"; target = "x86_64-unknown-linux-gnu"; formats = ["deb" "rpm" "tar.xz"]; }
33+
{ arch = "aarch64"; os = "linux"; target = "aarch64-unknown-linux-gnu"; formats = ["deb" "rpm" "tar.xz"]; }
34+
{ arch = "armv7"; os = "linux"; target = "armv7-unknown-linux-gnueabihf"; formats = ["tar.xz"]; }
35+
{ arch = "arm"; os = "linux"; target = "arm-unknown-linux-gnueabihf"; formats = ["tar.xz"]; }
36+
{ arch = "riscv32"; os = "linux"; target = "riscv32gc-unknown-linux-gnu"; formats = ["tar.xz"]; }
37+
{ arch = "riscv64"; os = "linux"; target = "riscv64gc-unknown-linux-gnu"; formats = ["tar.xz"]; }
38+
# MacOS
39+
{ arch = "x86_64"; os = "macos"; target = "x86_64-apple-darwin"; formats = ["tar.xz"]; }
40+
{ arch = "aarch64"; os = "macos"; target = "aarch64-apple-darwin"; formats = ["tar.xz"]; }
41+
# Windows
42+
{ arch = "x86_64"; os = "windows"; target = "x86_64-pc-windows-msvc"; formats = ["zip"]; }
43+
{ arch = "i686"; os = "windows"; target = "i686-pc-windows-msvc"; formats = ["zip"]; }
44+
];
45+
46+
mkDevShell = { arch, os, ... }: pkgs.mkShell {
47+
packages = with pkgs; [ rustc cargo ];
48+
shellHook = ''
49+
echo "DevShell for grhooks ${arch}-${os}"
50+
'';
51+
};
52+
53+
mkPackage = { arch, os, target }: let
54+
crossPkgs = mkCrossPkgs { inherit arch os; };
55+
in pkgs.rustPlatform.buildRustPackage {
56+
pname = "grhooks";
57+
version = "0.1.0";
58+
src = ./.;
59+
cargoLock.lockFile = ./Cargo.lock;
60+
61+
CARGO_BUILD_TARGET = target;
62+
HOST_CC = lib.optionalString (os != "windows") "${pkgs.stdenv.cc.nativePrefix}cc";
63+
TARGET_CC = lib.optionalString (os != "windows") "${crossPkgs.stdenv.cc.targetPrefix}cc";
64+
65+
buildInputs = with pkgs; [ ]
66+
++ lib.optionals (os == "linux") [ stdenv.cc glibc ]
67+
++ lib.optionals (os == "macos") [ clang darwin.apple_sdk.frameworks.CoreFoundation ];
2468
};
25-
toolchain = (pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml);
2669

27-
in {
28-
devShells.default = pkgs.mkShell {
29-
packages = with pkgs; [ cargo-typify toolchain ];
70+
generatedMatrixJson = builtins.toJSON (lib.flatten (map ({ arch, os, formats, ... }:
71+
map (format: {
72+
arch = arch;
73+
os = os;
74+
format = format;
75+
package = "${os}-${arch}";
76+
bundler = if format == "zip" then ".#toZip"
77+
else if format == "deb" then "github:NixOS/bundlers#toDEB"
78+
else if format == "rpm" then "github:NixOS/bundlers#toRPM"
79+
else ".#toTarball";
80+
}) formats
81+
) architectures));
82+
in
83+
{
84+
devShells = lib.listToAttrs (map ({ arch, os, target, ... }: {
85+
name = "${os}-${arch}";
86+
value = mkDevShell { inherit arch os target; };
87+
}) architectures) // {
88+
# Default Devshell
89+
default = mkDevShell {
90+
arch = "x86_64";
91+
os = "linux";
92+
target = "x86_64-unknown-linux-gnu";
93+
};
94+
};
95+
96+
packages = lib.listToAttrs (map ({ arch, os, target, ... }: {
97+
name = "${os}-${arch}";
98+
value = mkPackage { inherit arch os target; };
99+
}) architectures);
100+
101+
apps = {
102+
help = {
103+
type = "app";
104+
program = toString (pkgs.writeScript "help" ''
105+
#!/bin/sh
106+
echo ""
107+
echo "Welcome to Grhooks!"
108+
echo ""
109+
echo -e "\033[0;33mAvailable architectures:\033[0m"
110+
${lib.concatMapStringsSep "\n" (arch: ''echo " - ${arch}"'') (lib.lists.unique (map ({ arch, ... }: arch) architectures))}
111+
echo ""
112+
echo -e "\033[0;35mAvailable OS:\033[0m"
113+
${lib.concatMapStringsSep "\n" (os: ''echo " - ${os}"'') (lib.lists.unique (map ({ os, ... }: os) architectures))}
114+
echo ""
115+
echo -e "\033[0;32mTo build a specific variant, use:\033[0m"
116+
echo " nix build .#<os>-<arch>"
117+
echo ""
118+
echo -e "\033[0;32mExample:\033[0m"
119+
echo " nix build .#linux-x86_64"
120+
'');
121+
};
122+
123+
matrix = {
124+
type = "app";
125+
program = toString (pkgs.writeScript "generate-matrix" ''
126+
#!/bin/sh
127+
echo '${generatedMatrixJson}'
128+
'');
129+
};
130+
};
131+
132+
bundlers = let
133+
compress = ext: name: drv: pkgs.stdenv.mkDerivation {
134+
name = "${name}-${drv.name}";
135+
buildInputs = with pkgs; [ouch];
136+
unpackPhase = "true";
137+
buildPhase = ''
138+
export HOME=$PWD
139+
mkdir -p ./nix/store/
140+
mkdir -p ./bin
141+
for item in "$(cat ${pkgs.referencesByPopularity drv})"
142+
do
143+
cp -r $item ./nix/store/
144+
done
145+
146+
cp -r ${drv}/bin/* ./bin/
147+
148+
chmod -R a+rwx ./nix
149+
chmod -R a+rwx ./bin
150+
ouch c nix bin ${drv.name}.${ext}
151+
'';
152+
153+
installPhase = ''
154+
mkdir -p $out
155+
cp -r *.${ext} $out
156+
'';
157+
};
158+
in {
159+
toTarball = compress "tar.xz" "tar-simple";
160+
toZip = compress "zip" "zip-simple";
30161
};
31-
}
32-
);
162+
});
33163
}

0 commit comments

Comments
 (0)