From 77a6eb4f512b6d8ae94b82677d8efe3eacf8cfa8 Mon Sep 17 00:00:00 2001 From: sterni Date: Tue, 20 Feb 2024 17:49:17 +0100 Subject: feat(nix/writeTree): don't require IfD for drvs in tree As far as I can tell we can handle files and directories using the same cp(1) invocation, so we no longer need to potentially IfD derivations in the tree to figure out whether they are files or directories. Change-Id: Iabe648c30a747fa42768558715e388552024764a Reviewed-on: https://cl.tvl.fyi/c/depot/+/10996 Reviewed-by: aspen Autosubmit: sterni Tested-by: BuildkiteCI --- nix/writeTree/default.nix | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) (limited to 'nix/writeTree') diff --git a/nix/writeTree/default.nix b/nix/writeTree/default.nix index 43ece9b19f..0c7c2a130f 100644 --- a/nix/writeTree/default.nix +++ b/nix/writeTree/default.nix @@ -1,8 +1,12 @@ { depot, lib, pkgs, ... }: let - inherit (lib) fix pipe mapAttrsToList isAttrs concatLines isString; + inherit (lib) fix pipe mapAttrsToList isAttrs concatLines isString isDerivation isPath; - inherit (depot.nix.utils) isDirectory isRegularFile; + # TODO(sterni): move to //nix/utils with clearer naming and alternative similar to lib.types.path + isPathLike = value: + isPath value + || isDerivation value + || (isString value && builtins.hasContext value); esc = s: lib.escapeShellArg /* ensure paths import into store */ "${s}"; @@ -12,24 +16,12 @@ let '' + pipe tree [ (mapAttrsToList (k: v: - # TODO(sterni): a more discoverable isPathLike would fit into //nix/utils - # ATTN: This check has the flaw that it accepts paths without context - # that would not be available in the sandbox! - if lib.types.path.check v then - if isRegularFile v then - "cp --reflink=auto ${esc v} \"$out/\"${esc path}/${esc k}" - else if isDirectory v then '' - mkdir -p "$out/"${esc path} - cp -r --reflink=auto ${esc v} "$out/"${esc path}/${esc k} - '' - else - throw "invalid path type (expected file or directory)" - else if isAttrs v then - writeTreeAtPath "${path}/${k}" v - else if isString v then - "cp --reflink=auto ${esc v} \"$out/\"${esc path}/${esc k}" + if isPathLike v then + "cp -R --reflink=auto ${v} \"$out/\"${esc path}/${esc k}" + else if lib.isAttrs v then + writeTreeAtPath (path + "/" + k) v else - throw "invalid type (expected file, directory, or attrs)")) + throw "invalid type (expected path, derivation, string with context, or attrs)")) concatLines ]; -- cgit 1.4.1