Skip to content

Commit

Permalink
add workspace "target"
Browse files Browse the repository at this point in the history
makes shell and test-all a lot faster, since there is less evaluation
needed, and dependency deduplication
  • Loading branch information
henrispriet committed Sep 21, 2024
1 parent e48ccca commit b91c673
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 93 deletions.
28 changes: 19 additions & 9 deletions default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ let

inherit (pkgs) lib;

mkExample = pkgs.callPackage ./mk-example.nix {};
mkExample = pkgs.callPackage ./mk-example.nix { inherit workspace; };
mkTest = pkgs.callPackage ./mk-test.nix {};

examples = lib.mapAttrs
Expand All @@ -17,8 +17,12 @@ let
);
exampleDrvs = lib.attrValues examples;

# build the workspace
workspace = pkgs.callPackage ./workspace.nix {};
in {
# build all examples
inherit workspace;

# build all examples (individually, does not use workspace)
# recommended to build with --keep-going, to keep building other examples when one fails
# example use: `nix-build -A all --keep-going`
all = pkgs.symlinkJoin {
Expand All @@ -27,24 +31,30 @@ in {
};

# test all examples
# builds the whole workspace to test, to build all examples individually, use `nix-build -A all`
# recommended to build with --keep-going, to keep running other tests when one fails
# example use: `nix-build -A test-all --keep-going`
test-all = pkgs.symlinkJoin {
name = "all-tests";
paths = (builtins.map (mkTest "wayland") exampleDrvs)
++ (builtins.map (mkTest "x11") exampleDrvs);
paths = builtins.map
(args: mkTest (args // { pkg = workspace; }))
(lib.cartesianProduct {
displayServer = [ "x11" "wayland" ];
exe = lib.attrNames examples;
});
};

# test one example
# example use: `nix-build -A test --argstr displayServer wayland --argstr example arc`
# example use: `nix-build -A test.driverInteractive --argstr displayServer wayland --argstr example arc`
test = { displayServer, example }: mkTest displayServer examples.${example};
test = { displayServer, example }: mkTest {
inherit displayServer;
pkg = examples.${example};
exe = example;
};

shell = pkgs.mkShell {
inputsFrom = exampleDrvs;
inputsFrom = [ workspace ];
packages = with pkgs; [ rustfmt clippy ];

# FIXME: inputsFrom workspace instead?
# shellHook = ''echo "evaluating depencies for all examples, this could take a while..."'';
};
} // examples
77 changes: 13 additions & 64 deletions mk-example.nix
Original file line number Diff line number Diff line change
@@ -1,67 +1,16 @@
{
lib,
rustPlatform,
expat,
freetype,
libX11,
libxcb,
libXcursor,
libXi,
libxkbcommon,
libXrandr,
vulkan-loader,
wayland,
}:
{ lib, workspace }:
pname:
let
rpathLibs = [
libXcursor
libXi
libxkbcommon
libXrandr
libX11
vulkan-loader
wayland
];
in
rustPlatform.buildRustPackage {
inherit pname;
version = "0.1.0";
workspace.overrideAttrs {
inherit pname;
# HACK: think i need version override here because buildRustPackage checks that it is the same as in Cargo.toml
# probably a way to disable this, though i can't be bothered rn
version = "0.1.0";

src = let
fs = lib.fileset;
fileset = fs.difference
(fs.gitTracked ./.)
(fs.unions [
./npins
(fs.fileFilter (f: f.hasExt "nix") ./.)
]);
in
fs.toSource {
root = ./.;
inherit fileset;
};
# examples are actually individual sub-crates (because theyre in a workspace?)
# cargoBuildFlags = "--example ${pname}";
cargoBuildFlags = "--package ${pname}";

# examples are actually individual sub-crates (because theyre in a workspace?)
# cargoBuildFlags = "--example ${pname}";
cargoBuildFlags = "--package ${pname}";

cargoLock.lockFile = ./Cargo.lock;

buildInputs = [
expat
freetype
libxcb
libX11
libxkbcommon
];

fixupPhase = ''
patchelf --set-rpath "${lib.makeLibraryPath rpathLibs}" "$out/bin/${pname}";
'';

passthru = { inherit rpathLibs; };
# HACK: this is _could_ lead to some weird errors if this is wrong
# should be fine for the examples though
meta.mainProgram = pname;
}
fixupPhase = ''
patchelf --set-rpath "${lib.makeLibraryPath workspace.rpathLibs}" "$out/bin/${pname}";
'';
}
42 changes: 22 additions & 20 deletions mk-test.nix
Original file line number Diff line number Diff line change
Expand Up @@ -7,37 +7,39 @@ in
# adapted from https://github.com/NixOS/nixpkgs/blob/d0b8a0b81552dd2647de199a3d11775d25007bec/nixos/tests/freetube.nix
# tysm to kirillrdy tbh!!!
let
inherit (lib) getExe;

tests = {
wayland = package: {...}: {
wayland = program: {...}: {
imports = [ "${common}/wayland-cage.nix" ];
services.cage.program = getExe package;
services.cage.program = program;
virtualisation.memorySize = 2047;
environment.variables.NIXOS_OZONE_WL = "1";
environment.variables.DISPLAY = "do not use";
};
x11 = package: {...}: {
x11 = program: {...}: {
imports = [ "${common}/user-account.nix" "${common}/x11.nix" ];
virtualisation.memorySize = 2047;
services.xserver.enable = true;
services.xserver.displayManager.sessionCommands = getExe package;
services.xserver.displayManager.sessionCommands = program;
test-support.displayManager.auto.user = "alice";
};
};
in
displayServer: package:
nixosTest ({...}: {
name = "test-${package.name}-${displayServer}";
nodes = { machine = tests.${displayServer} package; };
# time-out on ofborg
# meta.broken = pkgs.stdenv.isAarch64;
enableOCR = true;
{
displayServer,
pkg,
exe,
}:
nixosTest ({...}: {
name = "test-${pkg.name}-${exe}-${displayServer}";
nodes = { machine = tests.${displayServer} (lib.getExe' pkg exe); };
# time-out on ofborg
# meta.broken = pkgs.stdenv.isAarch64;
enableOCR = true;

testScript = ''
start_all()
machine.wait_for_unit('graphical.target')
machine.sleep(3)
machine.screenshot("${package.name}-${displayServer}-screen")
'';
})
testScript = ''
start_all()
machine.wait_for_unit('graphical.target')
machine.sleep(3)
machine.screenshot("${exe}-${displayServer}-screen")
'';
})
70 changes: 70 additions & 0 deletions workspace.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
{
lib,
rustPlatform,
pkg-config,
expat,
freetype,
libX11,
libxcb,
libXcursor,
libXi,
libxkbcommon,
libXrandr,
vulkan-loader,
wayland,
}:
let
rpathLibs = [
libXcursor
libXi
libxkbcommon
libXrandr
libX11
vulkan-loader
wayland
];
in
rustPlatform.buildRustPackage {
pname = "iced-workspace";
version = "0.13.1";

src = let
fs = lib.fileset;
fileset = fs.difference
(fs.gitTracked ./.)
(fs.unions [
./npins
(fs.fileFilter (f: f.hasExt "nix") ./.)
]);
in
fs.toSource {
root = ./.;
inherit fileset;
};

cargoLock.lockFile = ./Cargo.lock;

nativeBuildInputs = [
pkg-config
];

buildInputs = [
expat
freetype
libxcb
libX11
libxkbcommon
];

# build all packages in workspace
cargoBuildFlags = "--workspace";

fixupPhase = ''
for example in examples/*/; do
exe=$(basename "$example")
patchelf --set-rpath "${lib.makeLibraryPath rpathLibs}" "$out/bin/$exe";
done
'';

passthru = { inherit rpathLibs; };
}

0 comments on commit b91c673

Please sign in to comment.