From 9137e671a2e2f5b46748ae21b21737b3f4194883 Mon Sep 17 00:00:00 2001 From: Jan Tojnar Date: Sun, 4 Sep 2022 12:36:11 +0200 Subject: [PATCH] update-script-combinators.copyAttrOutputToFile: init Useful for storing generated files into Nixpkgs repo as a part of an update script to avoid a build time dependency. --- pkgs/common-updater/combinators.nix | 38 ++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/pkgs/common-updater/combinators.nix b/pkgs/common-updater/combinators.nix index 93fdac52f7c4..d60a6e742715 100644 --- a/pkgs/common-updater/combinators.nix +++ b/pkgs/common-updater/combinators.nix @@ -19,7 +19,8 @@ command : (FilePath | [ (FilePath | String) ]) // Features that the script supports // - commit: (experimental) returns commit message in stdout - supportedFeatures : ?[ "commit" ] + // - silent: (experimental) returns no stdout + supportedFeatures : ?[ ("commit" | "silent") ] // Override attribute path detected by update.nix attrPath : ?String } @@ -118,11 +119,40 @@ rec { in let scripts = scriptsNormalized; - validateFeatures = ({ supportedFeatures, ... }: supportedFeatures == [ ]); + hasCommitSupport = lib.findSingle ({ supportedFeatures, ... }: supportedFeatures == [ "commit" ]) null null scripts != null; + validateFeatures = + if hasCommitSupport then + ({ supportedFeatures, ... }: supportedFeatures == [ "commit" ] || supportedFeatures == [ "silent" ]) + else + ({ supportedFeatures, ... }: supportedFeatures == [ ]); in - assert lib.assertMsg (lib.all validateFeatures scripts) "Combining update scripts with features enabled is currently unsupported."; + assert lib.assertMsg (lib.all validateFeatures scripts) "Combining update scripts with features enabled (other than a single script with “commit” and all other with “silent”) is currently unsupported."; assert lib.assertMsg (builtins.length (lib.unique (builtins.map ({ attrPath ? null, ... }: attrPath) scripts)) == 1) "Combining update scripts with different attr paths is currently unsupported."; - commandsToShellInvocation (builtins.map ({ command, ... }: command) scripts); + { + command = commandsToShellInvocation (builtins.map ({ command, ... }: command) scripts); + supportedFeatures = lib.optionals hasCommitSupport [ + "commit" + ]; + }; + + /* + copyAttrOutputToFile : String → FilePath → UpdateScript + EXPERIMENTAL! Simple update script that copies the output of Nix derivation built by `attr` to `path`. + */ + copyAttrOutputToFile = + attr: + path: + + { + command = [ + "sh" + "-c" + "cp --no-preserve=all \"$(nix-build -A ${attr})\" \"$0\" > /dev/null" + path + ]; + supportedFeatures = [ "silent" ]; + }; + }