diff --git a/devour/flake.nix b/devour/flake.nix new file mode 100644 index 0000000..8b51e64 --- /dev/null +++ b/devour/flake.nix @@ -0,0 +1,52 @@ +{ + inputs = { + nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; + systems.url = "github:nix-systems/default"; + flake-parts.url = "github:hercules-ci/flake-parts"; + flake = { }; + }; + outputs = inputs@{ flake-parts, systems, ... }: + flake-parts.lib.mkFlake { inherit inputs; } { + systems = import systems; + perSystem = { self', pkgs, lib, system, ... }: { + packages.default = + let + # Given a flake output key, how to get the buildable derivation for + # any of its attr values? + flakeSchema = { + perSystem = { + lookupFlake = k: lib.attrByPath [ k system ] { }; + getDrv = { + packages = _: x: [ x ]; + checks = _: x: [ x ]; + devShells = _: x: [ x ]; + apps = _: app: [ app.program ]; + legacyPackages = k: v: + if k == "homeConfigurations" + then + lib.mapAttrsToList (_: cfg: cfg.activationPackage) v + else [ ]; + }; + }; + flake = { + lookupFlake = k: lib.attrByPath [ k ] { }; + getDrv = { + nixosConfigurations = _: cfg: + lib.optional pkgs.stdenv.isLinux cfg.config.system.build.toplevel; + darwinConfigurations = _: cfg: + lib.optional pkgs.stdenv.isDarwin cfg.config.system.build.toplevel; + }; + }; + }; + paths = + lib.flip lib.mapAttrsToList flakeSchema (lvl: lvlSchema: + lib.flip lib.mapAttrsToList lvlSchema.getDrv (kind: getDrv: + lib.mapAttrsToList + getDrv + (lvlSchema.lookupFlake kind inputs.flake)) + ); + in + pkgs.writeText "devour-output" (lib.strings.concatLines (lib.lists.flatten paths)); + }; + }; +} diff --git a/flake.lock b/flake.lock index f4eaedb..7b2a1b9 100644 --- a/flake.lock +++ b/flake.lock @@ -5,11 +5,11 @@ "nixpkgs-lib": "nixpkgs-lib" }, "locked": { - "lastModified": 1680392223, - "narHash": "sha256-n3g7QFr85lDODKt250rkZj2IFS3i4/8HBU2yKHO3tqw=", + "lastModified": 1693611461, + "narHash": "sha256-aPODl8vAgGQ0ZYFIRisxYG5MOGSkIczvu2Cd8Gb9+1Y=", "owner": "hercules-ci", "repo": "flake-parts", - "rev": "dcc36e45d054d7bb554c9cdab69093debd91a0b5", + "rev": "7f53fdb7bdc5bb237da7fefef12d099e4fd611ca", "type": "github" }, "original": { @@ -20,11 +20,11 @@ }, "nixpkgs": { "locked": { - "lastModified": 1682109806, - "narHash": "sha256-d9g7RKNShMLboTWwukM+RObDWWpHKaqTYXB48clBWXI=", + "lastModified": 1693654884, + "narHash": "sha256-EqKKEl+IOS8TSjkt+xn1qGpsjnx5/ag33YNQ1+c7OuM=", "owner": "nixos", "repo": "nixpkgs", - "rev": "2362848adf8def2866fabbffc50462e929d7fffb", + "rev": "e7f35e03abd06a2faef6684d0de813370e13bda8", "type": "github" }, "original": { @@ -37,11 +37,11 @@ "nixpkgs-lib": { "locked": { "dir": "lib", - "lastModified": 1680213900, - "narHash": "sha256-cIDr5WZIj3EkKyCgj/6j3HBH4Jj1W296z7HTcWj1aMA=", + "lastModified": 1693471703, + "narHash": "sha256-0l03ZBL8P1P6z8MaSDS/MvuU8E75rVxe5eE1N6gxeTo=", "owner": "NixOS", "repo": "nixpkgs", - "rev": "e3652e0735fbec227f342712f180f4f21f0594f2", + "rev": "3e52e76b70d5508f3cec70b882a29199f4d1ee85", "type": "github" }, "original": { diff --git a/flake.nix b/flake.nix index 06ebadf..730a54f 100644 --- a/flake.nix +++ b/flake.nix @@ -3,52 +3,25 @@ nixpkgs.url = "github:nixos/nixpkgs/nixpkgs-unstable"; systems.url = "github:nix-systems/default"; flake-parts.url = "github:hercules-ci/flake-parts"; - flake = { }; }; outputs = inputs@{ flake-parts, systems, ... }: flake-parts.lib.mkFlake { inherit inputs; } { systems = import systems; perSystem = { self', pkgs, lib, system, ... }: { - packages.default = - let - # Given a flake output key, how to get the buildable derivation for - # any of its attr values? - flakeSchema = { - perSystem = { - lookupFlake = k: lib.attrByPath [ k system ] { }; - getDrv = { - packages = _: x: [ x ]; - checks = _: x: [ x ]; - devShells = _: x: [ x ]; - apps = _: app: [ app.program ]; - legacyPackages = k: v: - if k == "homeConfigurations" - then - lib.mapAttrsToList (_: cfg: cfg.activationPackage) v - else [ ]; - }; - }; - flake = { - lookupFlake = k: lib.attrByPath [ k ] { }; - getDrv = { - nixosConfigurations = _: cfg: - lib.optional pkgs.stdenv.isLinux cfg.config.system.build.toplevel; - darwinConfigurations = _: cfg: - lib.optional pkgs.stdenv.isDarwin cfg.config.system.build.toplevel; - }; - }; - }; - paths = - lib.flip lib.mapAttrsToList flakeSchema (lvl: lvlSchema: - lib.flip lib.mapAttrsToList lvlSchema.getDrv (kind: getDrv: - lib.mapAttrsToList - getDrv - (lvlSchema.lookupFlake kind inputs.flake)) - ); - in - pkgs.runCommand "devour-output" { inherit paths; } '' - echo -n $paths > $out - ''; + packages = rec { + default = pkgs.symlinkJoin { + name = "devour-flake"; + paths = [ + devour-flake + devour-flake-uncached + ]; + }; + + # Build all derivations in a flake (or download them from the cache) + devour-flake = pkgs.callPackage ./pkgs/devour-flake.nix { }; + # Only build all uncached derivations in flake + devour-flake-uncached = pkgs.callPackage ./pkgs/devour-flake-uncached.nix { }; + }; }; }; } diff --git a/pkgs/devour-flake-uncached.nix b/pkgs/devour-flake-uncached.nix new file mode 100644 index 0000000..cb1a190 --- /dev/null +++ b/pkgs/devour-flake-uncached.nix @@ -0,0 +1,43 @@ +{ coreutils-full +, findutils +, jq +, nix +, nix-build-uncached +, writeShellApplication +}: + +writeShellApplication { + name = "devour-flake-uncached"; + runtimeInputs = [ + coreutils-full + findutils + jq + nix + nix-build-uncached + ]; + text = '' + set -euo pipefail + + FLAKE="$1" + shift 1 || true + + outDir=$(mktemp -d devour-flake-uncached.XXXX) + cleanup() { + rm -rf "$outDir" + } + trap cleanup EXIT + + nix derivation show \ + ${../devour}#default \ + "$@" \ + -L \ + --reference-lock-file ${../flake.lock} \ + --override-input flake "$FLAKE" \ + | jq -r 'to_entries[].value.inputDrvs | to_entries[].key' \ + | ( + cd "$outDir" + 1>&2 xargs nix-build-uncached + find . -type l -exec readlink '{}' \; + ) + ''; +} diff --git a/default.nix b/pkgs/devour-flake.nix similarity index 82% rename from default.nix rename to pkgs/devour-flake.nix index 686c85d..44b154f 100644 --- a/default.nix +++ b/pkgs/devour-flake.nix @@ -9,9 +9,10 @@ writeShellApplication { FLAKE="$1" shift 1 || true - nix build ${./.}#default \ + nix build ${../devour}#default \ "$@" \ -L --no-link --print-out-paths \ + --reference-lock-file ${../flake.lock} \ --override-input flake "$FLAKE" \ | xargs cat '';