summary refs log tree commit diff
path: root/nixos/modules/system/etc
diff options
context:
space:
mode:
authorFlorian Jacob <projects+git@florianjacob.de>2018-11-13 10:13:23 +0100
committerdanbst <abcz2.uprola@gmail.com>2019-01-31 09:17:35 +0200
commitfc8e1745c01be9fca7ae511ffdd292b1ca22a3d5 (patch)
tree5963c4417af21b682204f0fe75474d0148376b8e /nixos/modules/system/etc
parent2e760494913ece46019ae5fa49b2ca80f06032fb (diff)
downloadnixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar.gz
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar.bz2
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar.lz
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar.xz
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.tar.zst
nixpkgs-fc8e1745c01be9fca7ae511ffdd292b1ca22a3d5.zip
nixos/etc: Make symlinks relative instead of absolute
so that the links can be followed if the NixOS installation is not mounted as filesystem root.
In particular, this makes /etc/os-release adhere to the standard:
https://www.freedesktop.org/software/systemd/man/os-release.html
Fixes #28833.
Diffstat (limited to 'nixos/modules/system/etc')
-rw-r--r--nixos/modules/system/etc/make-etc.sh15
-rw-r--r--nixos/modules/system/etc/setup-etc.pl12
2 files changed, 20 insertions, 7 deletions
diff --git a/nixos/modules/system/etc/make-etc.sh b/nixos/modules/system/etc/make-etc.sh
index 1ca4c3046f0..9c0520e92fc 100644
--- a/nixos/modules/system/etc/make-etc.sh
+++ b/nixos/modules/system/etc/make-etc.sh
@@ -10,6 +10,11 @@ users_=($users)
 groups_=($groups)
 set +f
 
+# Create relative symlinks, so that the links can be followed if
+# the NixOS installation is not mounted as filesystem root.
+# Absolute symlinks violate the os-release format
+# at https://www.freedesktop.org/software/systemd/man/os-release.html
+# and break e.g. systemd-nspawn and os-prober.
 for ((i = 0; i < ${#targets_[@]}; i++)); do
     source="${sources_[$i]}"
     target="${targets_[$i]}"
@@ -19,14 +24,14 @@ for ((i = 0; i < ${#targets_[@]}; i++)); do
         # If the source name contains '*', perform globbing.
         mkdir -p $out/etc/$target
         for fn in $source; do
-            ln -s "$fn" $out/etc/$target/
+            ln -s --relative "$fn" $out/etc/$target/
         done
 
     else
-        
+
         mkdir -p $out/etc/$(dirname $target)
         if ! [ -e $out/etc/$target ]; then
-            ln -s $source $out/etc/$target
+            ln -s --relative $source $out/etc/$target
         else
             echo "duplicate entry $target -> $source"
             if test "$(readlink $out/etc/$target)" != "$source"; then
@@ -34,13 +39,13 @@ for ((i = 0; i < ${#targets_[@]}; i++)); do
                 exit 1
             fi
         fi
-        
+
         if test "${modes_[$i]}" != symlink; then
             echo "${modes_[$i]}"  > $out/etc/$target.mode
             echo "${users_[$i]}"  > $out/etc/$target.uid
             echo "${groups_[$i]}" > $out/etc/$target.gid
         fi
-        
+
     fi
 done
 
diff --git a/nixos/modules/system/etc/setup-etc.pl b/nixos/modules/system/etc/setup-etc.pl
index eed20065087..6cbf0e17793 100644
--- a/nixos/modules/system/etc/setup-etc.pl
+++ b/nixos/modules/system/etc/setup-etc.pl
@@ -4,6 +4,7 @@ use File::Copy;
 use File::Path;
 use File::Basename;
 use File::Slurp;
+use File::Spec;
 
 my $etc = $ARGV[0] or die;
 my $static = "/etc/static";
@@ -12,7 +13,13 @@ sub atomicSymlink {
     my ($source, $target) = @_;
     my $tmp = "$target.tmp";
     unlink $tmp;
-    symlink $source, $tmp or return 0;
+    # Create relative symlinks, so that the links can be followed if
+    # the NixOS installation is not mounted as filesystem root.
+    # Absolute symlinks violate the os-release format
+    # at https://www.freedesktop.org/software/systemd/man/os-release.html
+    # and break e.g. systemd-nspawn and os-prober.
+    my $rel = File::Spec->abs2rel($source, dirname $target);
+    symlink $rel, $tmp or return 0;
     rename $tmp, $target or return 0;
     return 1;
 }
@@ -30,7 +37,8 @@ sub isStatic {
 
     if (-l $path) {
         my $target = readlink $path;
-        return substr($target, 0, length "/etc/static/") eq "/etc/static/";
+        my $rel = File::Spec->abs2rel("/etc/static", dirname $path);
+        return substr($target, 0, length $rel) eq $rel;
     }
 
     if (-d $path) {