nix-openwrt-imagebuilder/builder.nix

139 lines
3.8 KiB
Nix
Raw Permalink Normal View History

2022-04-27 02:34:09 +02:00
{ pkgs ? import <nixpkgs> {}
# OpenWRT release
, release ? "21.02.3"
# OpenWRT target
, target
# Hardware model
, profile
, variant ? "generic"
# Checksum of the `sha256sums` file
2022-04-27 22:25:32 +02:00
, sha256 ?
(
import ./hashes/${release}.nix
).${target}.${variant}.sha256
2022-04-27 18:25:01 +02:00
# Checksum of a feed's `Packages` file
2022-04-27 22:25:32 +02:00
, feedsSha256 ?
(
import ./hashes/${release}.nix
).${target}.${variant}.feedsSha256
2022-04-30 01:31:10 +02:00
# Manually specify packages' arch for OpenWRT<19 releases without profiles.json
, packagesArch ? null
2022-04-27 18:25:01 +02:00
# Extra OpenWRT packages (can be prefixed with "-")
, packages ? []
2022-06-23 18:47:26 +02:00
# Directory of extra files to include
2022-04-27 18:25:01 +02:00
, files ? null
2022-06-23 18:47:26 +02:00
# Attrset of files to include
, extraFiles ? { }
2022-04-27 18:25:01 +02:00
# Which services in /etc/init.d/ should be disabled
, disabledServices ? []
# Add to output name
, extraImageName ? "nix"
2022-04-27 02:34:09 +02:00
}:
with pkgs;
let
2022-04-28 00:42:02 +02:00
inherit (import ./files.nix {
2022-04-30 01:31:10 +02:00
inherit pkgs release target variant sha256 feedsSha256 packagesArch;
}) arch variantFiles profiles expandDeps corePackages packagesByFeed allPackages;
requiredPackages = (
profiles.default_packages or (
builtins.attrNames packagesByFeed.base
++ builtins.attrNames corePackages
)
++ profiles.profiles.${profile}.device_packages or []
++ packages
);
allRequiredPackages = expandDeps allPackages requiredPackages;
2022-04-27 02:34:09 +02:00
in
2022-04-27 02:40:55 +02:00
2022-04-27 02:34:09 +02:00
stdenv.mkDerivation {
2022-04-27 18:25:01 +02:00
name = lib.concatStringsSep "-" ([
"openwrt" release
] ++
lib.optional (extraImageName != null) extraImageName ++
[ target variant profile ]);
2022-04-27 02:34:09 +02:00
src = variantFiles."openwrt-imagebuilder-${release}-${target}-${variant}.${hostPlatform.uname.system}-${hostPlatform.uname.processor}.tar.xz";
postPatch = ''
2022-04-27 02:34:09 +02:00
patchShebangs scripts staging_dir/host/bin
substituteInPlace rules.mk \
--replace "SHELL:=/usr/bin/env bash" "SHELL:=${runtimeShell}"
grep -r usr/bin/env
'';
configurePhase =
let
installPackages = writeScript "install-openwrt-packages" (
lib.concatMapStrings (pname:
let
package = allPackages.${pname};
in
lib.optionalString
(package.type == "real")
"[ -e packages/${package.filename} ] || ln -s ${package.file} packages/${package.filename}\n"
) allRequiredPackages
);
in ''
${installPackages}
echo "src imagebuilder file:packages" > repositories.conf
'';
2022-04-27 02:34:09 +02:00
2022-04-28 02:56:44 +02:00
nativeBuildInputs =
[
zlib unzip bzip2
ncurses which rsync git file getopt wget
bash perl python3
] ++
lib.optional (!lib.versionAtLeast release "21") python2;
2022-04-28 02:56:44 +02:00
buildPhase = ''
${lib.optionalString (!lib.versionAtLeast release "19") ''
# Hack around broken check for gcc
touch staging_dir/host/.prereq-build
''}
${lib.optionalString (files != null)
# copy files to avoid making etc read-only
"cp -r --no-preserve=all ${files} files"
}
2022-06-23 18:47:26 +02:00
${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} \
2022-04-27 18:25:01 +02:00
PROFILE="${profile}" \
PACKAGES="${lib.concatStringsSep " " packages}" \
2022-06-23 18:47:26 +02:00
${lib.optionalString (files != null || extraFiles != { })
''FILES=./files''
2022-04-27 18:25:01 +02:00
} \
DISABLED_SERVICES="${lib.concatStringsSep " " disabledServices}" \
EXTRA_IMAGE_NAME="${extraImageName}"
'';
2022-04-27 02:34:09 +02:00
installPhase = ''
mkdir -p $out/nix-support
pushd bin/targets/${target}/${variant}
for src in *; do
dst="$out/$src"
cp -ar "$src" "$dst"
if [ -f "$dst" ]; then
filename=$(basename "$dst")
echo "file ''${filename##*.} $dst" >> $out/nix-support/hydra-build-products
fi
done
popd
2022-04-27 02:34:09 +02:00
'';
2022-04-27 02:59:05 +02:00
dontFixup = true;
2022-04-27 02:34:09 +02:00
}