2013-02-03 09:35:11 +00:00
|
|
|
# Management of static files in /etc.
|
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 15:41:38 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2006-12-11 15:32:10 +00:00
|
|
|
|
2009-01-02 16:06:52 +00:00
|
|
|
let
|
|
|
|
|
2021-09-25 12:32:58 +00:00
|
|
|
etc' = filter (f: f.enable) (attrValues config.environment.etc);
|
2013-02-03 12:24:22 +00:00
|
|
|
|
2021-08-07 18:56:21 +00:00
|
|
|
etc = pkgs.runCommandLocal "etc" {
|
2021-07-22 15:38:31 +00:00
|
|
|
# This is needed for the systemd module
|
|
|
|
passthru.targets = map (x: x.target) etc';
|
|
|
|
} /* sh */ ''
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
makeEtcEntry() {
|
|
|
|
src="$1"
|
|
|
|
target="$2"
|
|
|
|
mode="$3"
|
|
|
|
user="$4"
|
|
|
|
group="$5"
|
|
|
|
|
|
|
|
if [[ "$src" = *'*'* ]]; then
|
|
|
|
# If the source name contains '*', perform globbing.
|
|
|
|
mkdir -p "$out/etc/$target"
|
|
|
|
for fn in $src; do
|
|
|
|
ln -s "$fn" "$out/etc/$target/"
|
|
|
|
done
|
|
|
|
else
|
|
|
|
|
|
|
|
mkdir -p "$out/etc/$(dirname "$target")"
|
|
|
|
if ! [ -e "$out/etc/$target" ]; then
|
|
|
|
ln -s "$src" "$out/etc/$target"
|
|
|
|
else
|
|
|
|
echo "duplicate entry $target -> $src"
|
|
|
|
if [ "$(readlink "$out/etc/$target")" != "$src" ]; then
|
|
|
|
echo "mismatched duplicate entry $(readlink "$out/etc/$target") <-> $src"
|
|
|
|
ret=1
|
|
|
|
|
|
|
|
continue
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
if [ "$mode" != symlink ]; then
|
|
|
|
echo "$mode" > "$out/etc/$target.mode"
|
|
|
|
echo "$user" > "$out/etc/$target.uid"
|
|
|
|
echo "$group" > "$out/etc/$target.gid"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p "$out/etc"
|
|
|
|
${concatMapStringsSep "\n" (etcEntry: escapeShellArgs [
|
|
|
|
"makeEtcEntry"
|
2021-09-25 12:32:58 +00:00
|
|
|
# Force local source paths to be added to the store
|
|
|
|
"${etcEntry.source}"
|
2021-07-22 15:38:31 +00:00
|
|
|
etcEntry.target
|
|
|
|
etcEntry.mode
|
|
|
|
etcEntry.user
|
|
|
|
etcEntry.group
|
|
|
|
]) etc'}
|
|
|
|
'';
|
2013-02-03 09:35:11 +00:00
|
|
|
|
2024-05-25 12:41:24 +00:00
|
|
|
etcHardlinks = filter (f: f.mode != "symlink" && f.mode != "direct-symlink") etc';
|
2023-10-12 22:02:48 +00:00
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
in
|
|
|
|
|
|
|
|
{
|
|
|
|
|
2021-12-03 12:21:16 +00:00
|
|
|
imports = [ ../build.nix ];
|
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
###### interface
|
|
|
|
|
|
|
|
options = {
|
|
|
|
|
2023-10-12 22:02:48 +00:00
|
|
|
system.etc.overlay = {
|
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Mount `/etc` as an overlayfs instead of generating it via a perl script.
|
|
|
|
|
|
|
|
Note: This is currently experimental. Only enable this option if you're
|
|
|
|
confident that you can recover your system if it breaks.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
mutable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
|
|
|
description = ''
|
|
|
|
Whether to mount `/etc` mutably (i.e. read-write) or immutably (i.e. read-only).
|
|
|
|
|
|
|
|
If this is false, only the immutable lowerdir is mounted. If it is
|
|
|
|
true, a writable upperdir is mounted on top.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2009-05-28 13:17:56 +00:00
|
|
|
environment.etc = mkOption {
|
2013-02-03 12:24:22 +00:00
|
|
|
default = {};
|
2021-10-03 16:06:03 +00:00
|
|
|
example = literalExpression ''
|
2016-04-27 15:27:52 +00:00
|
|
|
{ example-configuration-file =
|
2013-02-03 12:24:22 +00:00
|
|
|
{ source = "/nix/store/.../etc/dir/file.conf.example";
|
|
|
|
mode = "0440";
|
|
|
|
};
|
|
|
|
"default/useradd".text = "GROUP=100 ...";
|
2013-10-30 15:19:07 +00:00
|
|
|
}
|
|
|
|
'';
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
|
|
|
Set of files that have to be linked in {file}`/etc`.
|
2009-05-28 13:17:56 +00:00
|
|
|
'';
|
2013-02-03 12:24:22 +00:00
|
|
|
|
2020-08-22 23:28:45 +00:00
|
|
|
type = with types; attrsOf (submodule (
|
2021-11-11 03:01:31 +00:00
|
|
|
{ name, config, options, ... }:
|
2013-02-03 12:24:22 +00:00
|
|
|
{ options = {
|
|
|
|
|
2013-02-03 13:19:05 +00:00
|
|
|
enable = mkOption {
|
|
|
|
type = types.bool;
|
|
|
|
default = true;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2013-02-03 13:19:05 +00:00
|
|
|
Whether this /etc file should be generated. This
|
|
|
|
option allows specific /etc files to be disabled.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 12:24:22 +00:00
|
|
|
target = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.str;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2013-02-03 12:24:22 +00:00
|
|
|
Name of symlink (relative to
|
2022-07-20 10:32:04 +00:00
|
|
|
{file}`/etc`). Defaults to the attribute
|
2013-02-03 12:24:22 +00:00
|
|
|
name.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 13:12:49 +00:00
|
|
|
text = mkOption {
|
|
|
|
default = null;
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.nullOr types.lines;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = "Text of the file.";
|
2013-02-03 13:12:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
source = mkOption {
|
2013-10-28 14:12:11 +00:00
|
|
|
type = types.path;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = "Path of the source file.";
|
2013-02-03 13:12:49 +00:00
|
|
|
};
|
|
|
|
|
2013-02-03 12:24:22 +00:00
|
|
|
mode = mkOption {
|
2013-10-30 16:37:45 +00:00
|
|
|
type = types.str;
|
2013-02-03 12:24:22 +00:00
|
|
|
default = "symlink";
|
|
|
|
example = "0600";
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
|
|
|
If set to something else than `symlink`,
|
2013-02-03 12:24:22 +00:00
|
|
|
the file is copied instead of symlinked, with the given
|
|
|
|
file mode.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2014-02-18 08:09:01 +00:00
|
|
|
uid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2020-03-09 11:01:19 +00:00
|
|
|
UID of created file. Only takes effect when the file is
|
2014-02-18 08:09:01 +00:00
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
gid = mkOption {
|
|
|
|
default = 0;
|
|
|
|
type = types.int;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2020-03-09 11:01:19 +00:00
|
|
|
GID of created file. Only takes effect when the file is
|
2014-02-18 08:09:01 +00:00
|
|
|
copied (that is, the mode is not 'symlink').
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2017-07-21 14:41:19 +00:00
|
|
|
user = mkOption {
|
|
|
|
default = "+${toString config.uid}";
|
|
|
|
type = types.str;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2017-07-21 14:41:19 +00:00
|
|
|
User name of created file.
|
2020-03-09 11:01:19 +00:00
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2022-07-20 10:32:04 +00:00
|
|
|
Changing this option takes precedence over `uid`.
|
2017-07-21 14:41:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
group = mkOption {
|
|
|
|
default = "+${toString config.gid}";
|
|
|
|
type = types.str;
|
2022-07-20 10:32:04 +00:00
|
|
|
description = ''
|
2017-07-21 14:41:19 +00:00
|
|
|
Group name of created file.
|
2020-03-09 11:01:19 +00:00
|
|
|
Only takes effect when the file is copied (that is, the mode is not 'symlink').
|
2022-07-20 10:32:04 +00:00
|
|
|
Changing this option takes precedence over `gid`.
|
2017-07-21 14:41:19 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2013-02-03 12:24:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
config = {
|
|
|
|
target = mkDefault name;
|
2015-12-30 14:13:43 +00:00
|
|
|
source = mkIf (config.text != null) (
|
2023-06-06 22:06:15 +00:00
|
|
|
let name' = "etc-" + lib.replaceStrings ["/"] ["-"] name;
|
2021-11-14 14:57:54 +00:00
|
|
|
in mkDerivedConfig options.text (pkgs.writeText name')
|
|
|
|
);
|
2013-02-03 12:24:22 +00:00
|
|
|
};
|
|
|
|
|
2016-09-11 10:35:42 +00:00
|
|
|
}));
|
2013-02-03 12:24:22 +00:00
|
|
|
|
2009-01-02 16:06:52 +00:00
|
|
|
};
|
2013-02-03 09:35:11 +00:00
|
|
|
|
2009-01-02 16:06:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
###### implementation
|
2009-08-16 13:14:33 +00:00
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
config = {
|
2009-08-16 13:14:33 +00:00
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
system.build.etc = etc;
|
2023-10-12 22:02:48 +00:00
|
|
|
system.build.etcActivationCommands = let
|
|
|
|
etcOverlayOptions = lib.concatStringsSep "," ([
|
|
|
|
"relatime"
|
|
|
|
"redirect_dir=on"
|
|
|
|
"metacopy=on"
|
|
|
|
] ++ lib.optionals config.system.etc.overlay.mutable [
|
|
|
|
"upperdir=/.rw-etc/upper"
|
|
|
|
"workdir=/.rw-etc/work"
|
|
|
|
]);
|
|
|
|
in if config.system.etc.overlay.enable then ''
|
|
|
|
# This script atomically remounts /etc when switching configuration. On a (re-)boot
|
|
|
|
# this should not run because /etc is mounted via a systemd mount unit
|
|
|
|
# instead. To a large extent this mimics what composefs does. Because
|
|
|
|
# it's relatively simple, however, we avoid the composefs dependency.
|
2024-02-21 21:54:00 +00:00
|
|
|
# Since this script is not idempotent, it should not run when etc hasn't
|
|
|
|
# changed.
|
|
|
|
if [[ ! $IN_NIXOS_SYSTEMD_STAGE1 ]] && [[ "${config.system.build.etc}/etc" != "$(readlink -f /run/current-system/etc)" ]]; then
|
2023-10-12 22:02:48 +00:00
|
|
|
echo "remounting /etc..."
|
|
|
|
|
|
|
|
tmpMetadataMount=$(mktemp --directory)
|
|
|
|
mount --type erofs ${config.system.build.etcMetadataImage} $tmpMetadataMount
|
|
|
|
|
|
|
|
# Mount the new /etc overlay to a temporary private mount.
|
|
|
|
# This needs the indirection via a private bind mount because you
|
|
|
|
# cannot move shared mounts.
|
|
|
|
tmpEtcMount=$(mktemp --directory)
|
|
|
|
mount --bind --make-private $tmpEtcMount $tmpEtcMount
|
|
|
|
mount --type overlay overlay \
|
|
|
|
--options lowerdir=$tmpMetadataMount::${config.system.build.etcBasedir},${etcOverlayOptions} \
|
|
|
|
$tmpEtcMount
|
|
|
|
|
|
|
|
# Move the new temporary /etc mount underneath the current /etc mount.
|
|
|
|
#
|
|
|
|
# This should eventually use util-linux to perform this move beneath,
|
|
|
|
# however, this functionality is not yet in util-linux. See this
|
|
|
|
# tracking issue: https://github.com/util-linux/util-linux/issues/2604
|
|
|
|
${pkgs.move-mount-beneath}/bin/move-mount --move --beneath $tmpEtcMount /etc
|
|
|
|
|
|
|
|
# Unmount the top /etc mount to atomically reveal the new mount.
|
|
|
|
umount /etc
|
|
|
|
|
|
|
|
fi
|
|
|
|
'' else ''
|
|
|
|
# Set up the statically computed bits of /etc.
|
|
|
|
echo "setting up /etc..."
|
|
|
|
${pkgs.perl.withPackages (p: [ p.FileSlurp ])}/bin/perl ${./setup-etc.pl} ${etc}/etc
|
|
|
|
'';
|
|
|
|
|
|
|
|
system.build.etcBasedir = pkgs.runCommandLocal "etc-lowerdir" { } ''
|
|
|
|
set -euo pipefail
|
|
|
|
|
|
|
|
makeEtcEntry() {
|
|
|
|
src="$1"
|
|
|
|
target="$2"
|
|
|
|
|
|
|
|
mkdir -p "$out/$(dirname "$target")"
|
|
|
|
cp "$src" "$out/$target"
|
|
|
|
}
|
|
|
|
|
|
|
|
mkdir -p "$out"
|
|
|
|
${concatMapStringsSep "\n" (etcEntry: escapeShellArgs [
|
|
|
|
"makeEtcEntry"
|
|
|
|
# Force local source paths to be added to the store
|
|
|
|
"${etcEntry.source}"
|
|
|
|
etcEntry.target
|
|
|
|
]) etcHardlinks}
|
|
|
|
'';
|
|
|
|
|
|
|
|
system.build.etcMetadataImage =
|
|
|
|
let
|
|
|
|
etcJson = pkgs.writeText "etc-json" (builtins.toJSON etc');
|
2024-04-21 08:55:03 +00:00
|
|
|
etcDump = pkgs.runCommand "etc-dump" { } ''
|
|
|
|
${lib.getExe pkgs.buildPackages.python3} ${./build-composefs-dump.py} ${etcJson} > $out
|
|
|
|
'';
|
2023-10-12 22:02:48 +00:00
|
|
|
in
|
|
|
|
pkgs.runCommand "etc-metadata.erofs" {
|
2024-04-21 08:55:03 +00:00
|
|
|
nativeBuildInputs = with pkgs.buildPackages; [ composefs erofs-utils ];
|
2023-10-12 22:02:48 +00:00
|
|
|
} ''
|
|
|
|
mkcomposefs --from-file ${etcDump} $out
|
|
|
|
fsck.erofs $out
|
2013-02-03 09:35:11 +00:00
|
|
|
'';
|
2023-10-12 22:02:48 +00:00
|
|
|
|
2013-02-03 09:35:11 +00:00
|
|
|
};
|
2009-10-30 08:37:08 +00:00
|
|
|
|
2007-02-26 21:18:13 +00:00
|
|
|
}
|