Skip to content

Commit d68c5e3

Browse files
authored
Fix and improve the nix flake (#512)
* fix(flake): `public` assets were moved to `kitsune/assets` Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * feat(flake): use mold linker Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * fix(flake): fix build dependency for openssl Native deps need the tools which run on the build host (when cross compiling). Normal build deps are the libs linked to the binary. OPENSSL_NO_VENDOR ensures that openssl-sys uses the system lib. Remove the rest of the workarounds for openssl. Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * fix(flake): proper `mkYarnPackage` Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * feat(flake): add overlay test Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * fix(flake): skip more tests Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * feat(flake): use the crane nix lib https://crane.dev/ enables caching of artifacts and potentially reducing build time. Even End to End(E2E) testing could be added: https://crane.dev/examples/end-to-end-testing.html Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * ci(flake): add nix github action Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * ci(flake): disable `cargo check` in nix flake Left as a separate commit, so it is easy to revert. Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * ci(flake): run nixci in debug build by default normal flake produces release code still. Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * ci(flake): build heavy stuff sequentially and skip running nixci. Signed-off-by: Harald Hoyer <harald@hoyer.xyz> * feat(flake): add `mrf-tool` and `kitsune-job-runner` Signed-off-by: Harald Hoyer <harald@hoyer.xyz> --------- Signed-off-by: Harald Hoyer <harald@hoyer.xyz>
1 parent d45e8f6 commit d68c5e3

File tree

6 files changed

+180
-45
lines changed

6 files changed

+180
-45
lines changed

.github/workflows/nix.yml

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
name: Nix checks
2+
3+
on:
4+
merge_group:
5+
pull_request:
6+
push:
7+
branches:
8+
- main
9+
workflow_dispatch:
10+
11+
env:
12+
CARGO_TERM_COLOR: always
13+
RUST_LOG: "debug"
14+
RUSTFLAGS: "-C debuginfo=0"
15+
16+
jobs:
17+
check:
18+
runs-on: ubuntu-latest
19+
steps:
20+
- uses: actions/checkout@v4
21+
- uses: DeterminateSystems/nix-installer-action@v4
22+
- uses: DeterminateSystems/magic-nix-cache-action@main
23+
- run: nix flake check -L --show-trace --keep-going --impure
24+
25+
build:
26+
needs: check
27+
runs-on: ubuntu-latest
28+
steps:
29+
- uses: actions/checkout@v4
30+
- uses: DeterminateSystems/nix-installer-action@v4
31+
- uses: DeterminateSystems/magic-nix-cache-action@main
32+
33+
- name: nix build main
34+
run: nix build --override-input debugBuild github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698 -L .#main
35+
- name: nix build cli
36+
run: nix build --override-input debugBuild github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698 -L .#cli
37+
- name: nix build frontend
38+
run: nix build --override-input debugBuild github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698 -L .#frontend
39+
- name: nix build mrf-tool
40+
run: nix build --override-input debugBuild github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698 -L .#mrf-tool
41+
42+
- name: nix check overlay
43+
run: cd test-overlay && nix build --no-write-lock-file -L .#kitsune
44+

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,5 @@ target-analyzer
2525
/result
2626
/.devenv
2727
/.pre-commit-config.yaml
28+
/test-overlay/flake.lock
29+
/test-overlay/result

flake.lock

+38
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flake.nix

+68-44
Original file line numberDiff line numberDiff line change
@@ -15,96 +15,113 @@
1515
};
1616
url = "github:oxalica/rust-overlay";
1717
};
18+
19+
crane = {
20+
url = "github:ipetkov/crane";
21+
inputs.nixpkgs.follows = "nixpkgs";
22+
};
23+
24+
# The premise is this is the "default" and if you want to do a debug build,
25+
# pass it in as an arg.
26+
# like so `nix build --override-input debugBuild github:boolean-option/true`
27+
debugBuild.url = "github:boolean-option/false/d06b4794a134686c70a1325df88a6e6768c6b212";
1828
};
19-
outputs = { self, devenv, flake-utils, nixpkgs, rust-overlay, ... } @ inputs:
20-
flake-utils.lib.eachDefaultSystem
29+
outputs = { self, devenv, flake-utils, nixpkgs, rust-overlay, crane, ... } @ inputs:
30+
(flake-utils.lib.eachDefaultSystem
2131
(system:
2232
let
33+
features = "--all-features";
2334
overlays = [ (import rust-overlay) ];
2435
pkgs = import nixpkgs {
2536
inherit overlays system;
2637
};
38+
stdenv = pkgs.stdenvAdapters.useMoldLinker pkgs.stdenv;
2739
rustPlatform = pkgs.makeRustPlatform {
2840
cargo = pkgs.rust-bin.stable.latest.minimal;
2941
rustc = pkgs.rust-bin.stable.latest.minimal;
42+
inherit stdenv;
3043
};
31-
baseDependencies = with pkgs; [
44+
45+
craneLib = (crane.mkLib pkgs).overrideToolchain pkgs.rust-bin.stable.latest.minimal;
46+
buildInputs = with pkgs; [
3247
openssl
33-
pkg-config
34-
protobuf
3548
sqlite
3649
zlib
3750
];
3851

39-
cargoConfig = builtins.fromTOML (builtins.readFile ./.cargo/config.toml); # TODO: Set the target CPU conditionally
40-
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
52+
nativeBuildInputs = with pkgs; [
53+
protobuf
54+
pkg-config
55+
rustPlatform.bindgenHook
56+
];
57+
4158
src = pkgs.lib.cleanSourceWith {
4259
src = pkgs.lib.cleanSource ./.;
4360
filter = name: type:
4461
let baseName = baseNameOf (toString name);
4562
in !(baseName == "flake.lock" || pkgs.lib.hasSuffix ".nix" baseName);
4663
};
47-
version = cargoToml.workspace.package.version;
4864

49-
basePackage = {
50-
inherit version src;
65+
commonArgs = {
66+
inherit src stdenv buildInputs nativeBuildInputs;
67+
68+
strictDeps = true;
5169

5270
meta = {
5371
description = "ActivityPub-federated microblogging";
5472
homepage = "https://joinkitsune.org";
5573
};
5674

57-
cargoLock = {
58-
lockFile = ./Cargo.lock;
59-
allowBuiltinFetchGit = true;
60-
};
75+
OPENSSL_NO_VENDOR = 1;
76+
NIX_OUTPATH_USED_AS_RANDOM_SEED = "aaaaaaaaaa";
77+
cargoExtraArgs = "--locked ${features}";
78+
} // (pkgs.lib.optionalAttrs inputs.debugBuild.value {
79+
# do a debug build, as `dev` is the default debug profile
80+
CARGO_PROFILE = "dev";
81+
});
6182

62-
nativeBuildInputs = baseDependencies;
63-
64-
PKG_CONFIG_PATH = "${pkgs.openssl.dev}/lib/pkgconfig"; # Not sure why this is broken but it is
65-
RUSTFLAGS = builtins.concatStringsSep " " cargoConfig.build.rustflags; # Oh god help.
66-
67-
checkFlags = [
68-
# Depend on creating an HTTP client and that reads from the systems truststore
69-
# Because nix is fully isolated, these types of tests fail
70-
#
71-
# Some (most?) of these also depend on the network? Not good??
72-
"--skip=activitypub::fetcher::test::federation_allow"
73-
"--skip=activitypub::fetcher::test::federation_deny"
74-
"--skip=activitypub::fetcher::test::fetch_actor"
75-
"--skip=activitypub::fetcher::test::fetch_note"
76-
"--skip=resolve::post::test::parse_mentions"
77-
"--skip=webfinger::test::fetch_qarnax_ap_id"
78-
"--skip=basic_request"
79-
"--skip=json_request"
80-
];
81-
};
83+
cargoToml = builtins.fromTOML (builtins.readFile ./Cargo.toml);
84+
version = cargoToml.workspace.package.version;
85+
86+
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {
87+
pname = "kitsune-workspace";
88+
src = craneLib.cleanCargoSource src;
89+
});
8290
in
8391
{
8492
formatter = pkgs.nixpkgs-fmt;
8593
packages = rec {
86-
# Hack to make latest devenv work
87-
devenv-up = self.devShells.${system}.default.config.procfileScript;
88-
8994
default = main;
9095

91-
cli = rustPlatform.buildRustPackage (basePackage // {
96+
cli = craneLib.buildPackage (commonArgs // {
9297
pname = "kitsune-cli";
93-
cargoBuildFlags = "-p kitsune-cli";
98+
cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune-cli";
99+
inherit cargoArtifacts;
100+
doCheck = false;
101+
});
102+
103+
mrf-tool = craneLib.buildPackage (commonArgs // {
104+
pname = "mrf-tool";
105+
cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin mrf-tool";
106+
inherit cargoArtifacts;
107+
doCheck = false;
94108
});
95109

96-
main = rustPlatform.buildRustPackage (basePackage // {
110+
main = craneLib.buildPackage (commonArgs // rec {
97111
pname = "kitsune";
98-
buildFeatures = [ "meilisearch" "oidc" ];
99-
cargoBuildFlags = "-p kitsune";
112+
cargoExtraArgs = commonArgs.cargoExtraArgs + " --bin kitsune --bin kitsune-job-runner";
113+
inherit cargoArtifacts;
114+
doCheck = false;
100115
});
101116

102117
frontend = pkgs.mkYarnPackage {
103118
inherit version;
104-
119+
packageJSON = "${src}/kitsune-fe/package.json";
120+
yarnLock = "${src}/kitsune-fe/yarn.lock";
105121
src = "${src}/kitsune-fe";
106122

107123
buildPhase = ''
124+
export HOME=$(mktemp -d)
108125
yarn --offline build
109126
'';
110127

@@ -131,7 +148,7 @@
131148
rust-bin.stable.latest.default
132149
]
133150
++
134-
baseDependencies;
151+
buildInputs ++ nativeBuildInputs;
135152

136153
enterShell = ''
137154
export PG_HOST=127.0.0.1
@@ -171,5 +188,12 @@
171188
default = kitsune;
172189
kitsune = (import ./module.nix);
173190
};
191+
}) // {
192+
nixci.default = {
193+
debug = {
194+
dir = ".";
195+
overrideInputs.debugBuild = "github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698";
196+
};
197+
};
174198
};
175199
}

overlay.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ in
99
installPhase = ''
1010
mkdir -p $out
1111
cp -R ${packages.main}/bin $out
12-
cp -R ${packages.main.src}/public $out
12+
cp -R ${packages.main.src}/kitsune/assets $out/public
1313
cp -R ${packages.frontend}/dist $out/kitsune-fe
1414
'';
1515
};

test-overlay/flake.nix

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
inputs = {
3+
kitsune-overlay.url = "./..";
4+
kitsune-overlay.inputs.debugBuild.follows = "debugBuild";
5+
nixpkgs.follows = "kitsune-overlay/nixpkgs";
6+
flake-utils.follows = "kitsune-overlay/flake-utils";
7+
debugBuild.url = "github:boolean-option/true/6ecb49143ca31b140a5273f1575746ba93c3f698";
8+
};
9+
outputs = { self, flake-utils, nixpkgs, kitsune-overlay, ... } @ inputs:
10+
flake-utils.lib.eachDefaultSystem
11+
(system:
12+
let
13+
overlays = [ kitsune-overlay.overlays.default ];
14+
pkgs = import nixpkgs {
15+
inherit overlays system;
16+
};
17+
in
18+
{
19+
formatter = pkgs.nixpkgs-fmt;
20+
packages = rec {
21+
default = kitsune;
22+
inherit (pkgs) kitsune;
23+
inherit (pkgs) kitsune-cli;
24+
};
25+
}
26+
);
27+
}

0 commit comments

Comments
 (0)