fix bug: use numeric UID/GID for directory ownership in initrd where usernames don't resolve#316
Open
mihakrumpestar wants to merge 1 commit intonix-community:masterfrom
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
When using
boot.initrd.systemd.enable = true, activation scripts run in the initrd environment where/etc/passwddoesn't contain regular users. This causescreate-directories.bashto fail when trying to set ownership with usernames likechown user:group /path, resulting in directories being created withroot:rootownership instead of the correct user ownership.This is particularly problematic for persistent home directories which are bind-mounted early in boot and need correct ownership for the user to access their files.
Note that this happens with both home-manager and nixos module, for all non-root users, when
initrd.systemdis enabled.Relevant sections in nix config:
I discovered the issue when I tried to make the directory creation script automatically fix the wrong user:group ownership.
journalctl-default.log
The relevant section from journalctl clearly shows that it tries to chown without the users even being available (they are created/available later in boot process).
Solution
Pass numeric UID/GID to the directory creation script and use them for
chownoperations when usernames cannot be resolved:uidandgidargumentschown uid:gidwhen the user doesn't existchown user:groupwhen the user database is availableboot.initrd.systemd.enable = true, require that users with persistent home directories have explicituid/gidsetRelevant section in journalctl that shows that the solution is working:
journalctl-with-solution.log
Changes
nixos.nix: Pass uid/gid to create-directories.bash, add assertion for initrd systemd userscreate-directories.bash: Accept uid/gid args, use numeric IDs when usernames don't resolvenixos-install(users don't exist in chroot anyway)Requirements for users
When using
boot.initrd.systemd.enable = truewith persistent home directories, users must now set explicit uid/gid:An assertion error is shown if this is forgotten.
Additional notes
While I did search the issues and pull request, none seem to mention this specific problem. If this fix is unnecessary, and I simply did something wrong in the config, please correct me.
I also added code that will fix the permissions if they got set wrong, but it is not really necessary, unless they got set wrong before.
Both journalctl logs are from first boot on completely fresh nixos system (used disko and nixos-install). I did not test with
initrd.systemddisabled, since I currently use all system drives encrypted only (initrd.systemdhas to be enabled therefore).