upower: Simplify DESTDIR hack

Let’s point `DESTDIR` to a path under the build directory rather than under "$out".
This will prevent `installPhase` from creating `$out` or any other output directory,
allowing us to install the outputs just by moving them out of `$DESTDIR` directly into the store with `mv`.
Of course, that will also require moving the installing of `etc` and `var` after the outputs are installed
and ensuring there is no library litter from `checkPhase`.

Since `$out` does not currently contain `var` subdirectory, we can also just move `$DESTDIR/var` directly to `$out` rather than copying it.
`$out/etc` does exist but we can just merge it with `cp --recursive` instead of `rsync` so `rsync` is not actually necessary.

The code remains written defensively against files accidentally being misplaced while shuffling them around – parent directories of targets are used as `mv` destinations so that the move fails loudly if the directory already exists, rather than being moved inside as e.g. `$out/etc/etc`.


While at it let’s also improve practices a bit:
- Add vertical space and clearer comments.
- Handle non-standard Nix store paths.
This commit is contained in:
Jan Tojnar 2023-11-19 18:24:30 +01:00
parent ff61f2bb3f
commit 8ef5fcf4f5

View File

@ -3,7 +3,6 @@
, fetchFromGitLab
, makeWrapper
, pkg-config
, rsync
, libxslt
, meson
, ninja
@ -69,7 +68,6 @@ stdenv.mkDerivation (finalAttrs: {
libxslt
makeWrapper
pkg-config
rsync
glib
] ++ lib.optionals withIntrospection [
gobject-introspection
@ -138,7 +136,6 @@ stdenv.mkDerivation (finalAttrs: {
# Our gobject-introspection patches make the shared library paths absolute
# in the GIR files. When running tests, the library is not yet installed,
# though, so we need to replace the absolute path with a local one during build.
# We are using a symlink that will be overwitten during installation.
mkdir -p "$out/lib"
ln -s "$PWD/libupower-glib/libupower-glib.so" "$out/lib/libupower-glib.so.3"
'';
@ -159,21 +156,28 @@ stdenv.mkDerivation (finalAttrs: {
# meson rebuild during install and it is not used at runtime anyway.
sed -Ei 's~#!.+/bin/python3~#!/usr/bin/python3~' \
../src/linux/integration-test.py
# Undo preCheck installation since DESTDIR hack expects outputs to not exist.
rm "$out/lib/libupower-glib.so.3"
rmdir "$out/lib" "$out"
'';
postInstall = ''
# Move stuff from DESTDIR to proper location.
# We use rsync to merge the directories.
for dir in etc var; do
rsync --archive "$DESTDIR/$dir" "$out"
rm --recursive "$DESTDIR/$dir"
for o in $(getAllOutputNames); do
# devdoc is created later by _multioutDocs hook.
if [[ "$o" = "devdoc" ]]; then continue; fi
mv "$DESTDIR''${!o}" "$(dirname "''${!o}")"
done
for o in out dev installedTests; do
rsync --archive "$DESTDIR/''${!o}" "$(dirname "''${!o}")"
rm --recursive "$DESTDIR/''${!o}"
done
# Ensure the DESTDIR is removed.
rmdir "$DESTDIR/nix/store" "$DESTDIR/nix" "$DESTDIR"
mv "$DESTDIR/var" "$out"
# The /etc already exist so we need to merge it.
cp --recursive "$DESTDIR/etc" "$out"
rm --recursive "$DESTDIR/etc"
# Ensure we did not forget to install anything.
rmdir --parents --ignore-fail-on-non-empty "$DESTDIR${builtins.storeDir}"
! test -e "$DESTDIR"
'';
postFixup = ''
@ -194,7 +198,7 @@ stdenv.mkDerivation (finalAttrs: {
# at install time but Meson does not support this
# so we need to convince it to install all files to a temporary
# location using DESTDIR and then move it to proper one in postInstall.
DESTDIR = "${placeholder "out"}/dest";
DESTDIR = "dest";
};
passthru = {