From 2cd689783a0dede6300299228e977d1f8b20d89c Mon Sep 17 00:00:00 2001 From: Emery Hemingway Date: Thu, 23 Jun 2022 11:47:26 -0500 Subject: [PATCH] Add extraFiles option --- builder.nix | 19 +++++++++++++++++-- lib/modules/build.nix | 2 +- lib/modules/files.nix | 29 +++++++++++++++++++++++++++-- 3 files changed, 45 insertions(+), 5 deletions(-) diff --git a/builder.nix b/builder.nix index 1045b2c..302c044 100644 --- a/builder.nix +++ b/builder.nix @@ -20,8 +20,10 @@ , packagesArch ? null # Extra OpenWRT packages (can be prefixed with "-") , packages ? [] -# Include extra files +# Directory of extra files to include , files ? null +# Attrset of files to include +, extraFiles ? { } # Which services in /etc/init.d/ should be disabled , disabledServices ? [] # Add to output name @@ -95,10 +97,23 @@ stdenv.mkDerivation { # copy files to avoid making etc read-only "cp -r --no-preserve=all ${files} files" } + ${lib.strings.concatStringsSep "\n" ( + lib.attrsets.mapAttrsToList ( + path: { source, text }: '' + mkdir -p $(dirname ./files/${path}) + ${ + if text != null then '' + cat << NIX_EOF > ./files/${path} + ${text} + NIX_EOF + '' else "cat < ${source} > ./files/${path}"} + '' + ) extraFiles) + } make image SHELL=${runtimeShell} \ PROFILE="${profile}" \ PACKAGES="${lib.concatStringsSep " " packages}" \ - ${lib.optionalString (files != null) + ${lib.optionalString (files != null || extraFiles != { }) ''FILES=./files'' } \ DISABLED_SERVICES="${lib.concatStringsSep " " disabledServices}" \ diff --git a/lib/modules/build.nix b/lib/modules/build.nix index 108b159..1b7a850 100644 --- a/lib/modules/build.nix +++ b/lib/modules/build.nix @@ -4,7 +4,7 @@ system.build.image = import ../../builder.nix { inherit pkgs; inherit (config.system) release target variant profile; - inherit (config) files; + inherit (config) files extraFiles; packages = config.packages.include ++ map (x: "-${x}") config.packages.exclude; disabledServices = config.services.disabled; diff --git a/lib/modules/files.nix b/lib/modules/files.nix index ec9c3e5..d8359b3 100644 --- a/lib/modules/files.nix +++ b/lib/modules/files.nix @@ -1,8 +1,9 @@ { lib, ... }: { - options.files = with lib; - mkOption { + options = with lib; { + + files = mkOption { type = types.path; default = null; example = literalExample '' @@ -18,4 +19,28 @@ ''; description = "Directory of files to included in images."; }; + + extraFiles = mkOption { + default = { }; + description = '' + Set of files to build into the image. + ''; + type = with types; + attrsOf (submodule { + options = { + source = mkOption { + default = null; + type = types.path; + description = "Path of the source file."; + }; + text = mkOption { + default = null; + type = types.nullOr types.lines; + description = "Text of the file."; + }; + }; + }); + }; + + }; }