From b91c6738705be25cc820fd142eea670b417b2df0 Mon Sep 17 00:00:00 2001 From: henrispriet Date: Sat, 21 Sep 2024 13:49:57 +0200 Subject: [PATCH] add workspace "target" makes shell and test-all a lot faster, since there is less evaluation needed, and dependency deduplication --- default.nix | 28 ++++++++++++------ mk-example.nix | 77 +++++++++----------------------------------------- mk-test.nix | 42 ++++++++++++++------------- workspace.nix | 70 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 93 deletions(-) create mode 100644 workspace.nix diff --git a/default.nix b/default.nix index 01096e0d53..1eb3458533 100644 --- a/default.nix +++ b/default.nix @@ -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 @@ -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 { @@ -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 diff --git a/mk-example.nix b/mk-example.nix index 18c2619228..94699905df 100644 --- a/mk-example.nix +++ b/mk-example.nix @@ -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}"; + ''; +} diff --git a/mk-test.nix b/mk-test.nix index ea653cee8f..326f986701 100644 --- a/mk-test.nix +++ b/mk-test.nix @@ -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") + ''; +}) diff --git a/workspace.nix b/workspace.nix new file mode 100644 index 0000000000..46ceaa4291 --- /dev/null +++ b/workspace.nix @@ -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; }; + }