From 1d5ebbce4637db0e6915a6211d757692eddba8f7 Mon Sep 17 00:00:00 2001 From: jacekpoz Date: Fri, 13 Sep 2024 11:31:31 +0200 Subject: [PATCH] allow extraFiles to contain paths --- README.md | 9 ++++++--- nte-drv.nix | 13 +++++++++---- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 35b0d07..de7d2cc 100644 --- a/README.md +++ b/README.md @@ -51,9 +51,11 @@ it accepts an attrset of: - `extraArgs` - an attrset of additional arguments passed to all entries and templates - `entries` - a list of all entry files to be processed - `templates` - a list of all template files to be applied -- `extraFiles` - a list of attrsets of `source` and `destination`: - - `source` - a path, if relative `$PWD` is `$src` in the `installPhase` - - `destination` - a path, never absolute, appended to `$out` in the `installPhase` +- `extraFiles` - a list of either: + - a path to the source file - will be copied to `$out` in the `installPhase` + - attrset of `source` and `destination`: + - `source` - a path, if relative `$PWD` is `$src` in the `installPhase` + - `destination` - a path, never absolute, appended to `$out` in the `installPhase` example usage of the wrapper function: ```nix @@ -86,6 +88,7 @@ mkNteDerivation { ]; extraFiles = [ + ./data.txt # equivalent to { source = ./data.txt; destination = "/"; } { source = ./image.png; destination = "/assets/"; } { source = ./image2.png; destination = "/assets/dupa.png"; } { source = "./data/*"; destination = "/assets/data/"; } diff --git a/nte-drv.nix b/nte-drv.nix index 77b1bde..c0768a4 100644 --- a/nte-drv.nix +++ b/nte-drv.nix @@ -8,6 +8,7 @@ pkgs: engine: { extraFiles ? [] }: let inherit (pkgs) lib; + inherit (lib.attrsets) isAttrs; inherit (lib.lists) forEach init; inherit (lib.strings) concatStrings concatStringsSep match normalizePath optionalString splitString; in pkgs.stdenv.mkDerivation { @@ -28,12 +29,16 @@ in pkgs.stdenv.mkDerivation { ${concatStrings (forEach extraFiles (extraFile: let - isInSubdir = (match ".+/.*" extraFile.destination) != null; - outDir = normalizePath "$out/${concatStringsSep "/" (init (splitString "/" extraFile.destination))}"; - outPath = normalizePath "$out/${extraFile.destination}"; + fileAttrs = if isAttrs extraFile + then extraFile + else { source = extraFile; destination = "/"; }; + + isInSubdir = (match ".+/.*" fileAttrs.destination) != null; + outDir = normalizePath "$out/${concatStringsSep "/" (init (splitString "/" fileAttrs.destination))}"; + outPath = normalizePath "$out/${fileAttrs.destination}"; in /*sh*/'' ${optionalString isInSubdir /*sh*/"mkdir -p ${outDir}"} - cp -r ${extraFile.source} ${outPath} + cp -r ${fileAttrs.source} ${outPath} '')) }