Skip to content

nixos-rebuild: log output to systemd journal - #147272

Closed
Artturin wants to merge 6 commits into
NixOS:masterfrom
Artturin:rebuildsystemdcat
Closed

Artturin wants to merge 6 commits into
NixOS:masterfrom
Artturin:rebuildsystemdcat

Conversation

@Artturin

@Artturin Artturin commented Nov 24, 2021 •

Copy link
Copy Markdown
Member

can be viewed with journalctl -t nixos-rebuild

Motivation for this change

This will help with debugging
maybe helps with issues such as #146727

Things done

tested with nix build ".#nixos-rebuild" && sudo ./result/bin/nixos-rebuild switch
and

#!/usr/bin/env bash

# save all output to systemd journal
# 'journalctl -t nixos-rebuild' to inspect
# tee is used so we still get output to the terminal
# check if systemd-cat exists and if systemctl exits with 0. if it does not
# then systemd isn't running(chroot) and we cannot use systemd-cat.
command -v systemd-cat &>/dev/null && systemctl &>/dev/null && exec > >(tee >(systemd-cat -t nixos-rebuild)) 2>&1

echo hello123
echo $RANDOM
echo goestostderr >&2
  • Built on platform(s)
    • x86_64-linux
    • aarch64-linux
    • x86_64-darwin
    • aarch64-darwin
  • For non-Linux: Is sandbox = true set in nix.conf? (See Nix manual)
  • Tested, as applicable:
  • Tested compilation of all packages that depend on this change using nix-shell -p nixpkgs-review --run "nixpkgs-review rev HEAD". Note: all changes have to be committed, also see nixpkgs-review usage
  • Tested basic functionality of all binary files (usually in ./result/bin/)
  • 21.11 Release Notes (or backporting 21.05 Release notes)
    • (Package updates) Added a release notes entry if the change is major or breaking
    • (Module updates) Added a release notes entry if the change is significant
    • (Module addition) Added a release notes entry if adding a new NixOS module
    • (Release notes changes) Ran nixos/doc/manual/md-to-db.sh to update generated release notes
  • Fits CONTRIBUTING.md.

@Artturin
Artturin requested review from grahamc and roberth November 24, 2021 16:48
@github-actions github-actions Bot added 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation labels Nov 24, 2021
@roberth

roberth commented Nov 24, 2021

Copy link
Copy Markdown
Member

Wouldn't it be sufficient to log the output of the activation script?
That keeps failed builds etc out of the system log, which is good because those don't affect the system anyway.
What happens when nixos-rebuild build or nixos-rebuild switch run as user?

@Artturin

Copy link
Copy Markdown
Member Author

Wouldn't it be sufficient to log the output of the activation script? That keeps failed builds etc out of the system log, which is good because those don't affect the system anyway.

i'll test

What happens when nixos-rebuild build or nixos-rebuild switch run as user?

in journalctl

# -rebuild build
building Nix...
building the system configuration...
# -rebuild switch
building Nix...
building the system configuration...
error: creating symlink from '/nix/var/nix/profiles/.0_system' to 'system-165-link': Permission denied

so nothing special

@dasJ

dasJ commented Nov 24, 2021

Copy link
Copy Markdown
Member

@roberth but only logging the activation will not log started/stopped units :/ It's probably best to start logging just before the switch-to-configuration.pl call

@roberth

roberth commented Nov 24, 2021

Copy link
Copy Markdown
Member

@dasJ You're right. It should including switching, which is more than activation.
More specifically we should log all the mutation and none of the building, argument handling, etc.

@Artturin

Artturin commented Nov 24, 2021 •

Copy link
Copy Markdown
Member Author

in the temp commit the systemd-cat output is

updating GRUB 2 menu...
activating the configuration...
setting up /etc...
reloading user units for user...
setting up tmpfiles

but only if --fast is used

i think the cause is

# Re-execute nixos-rebuild from the Nixpkgs tree.
# FIXME: get nixos-rebuild from $flake.
if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast && -z $flake ]]; then
    if p=$(nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
        export _NIXOS_REBUILD_REEXEC=1
        exec "$p/bin/nixos-rebuild" "${origArgs[@]}"
        exit 1
    fi
fi

specifically the =p line because my system channel has the unmodified nixos-rebuild

i'll add 1>&3 2>&3 to other possible error sources too

@ofborg ofborg Bot added 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux. labels Nov 24, 2021
@Artturin

Artturin commented Nov 25, 2021 •

Copy link
Copy Markdown
Member Author

fixed it. apply this patch to test it

journalctl -t nixos-rebuild --follow can be used to view the journal auto refreshing

nix build ".#nixos-rebuild" && sudo ./result/bin/nixos-rebuild switch

diff --git a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh
index 5ca0a847d05..21276ce8187 100755
--- a/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh
+++ b/pkgs/os-specific/linux/nixos-rebuild/nixos-rebuild.sh
@@ -327,7 +327,7 @@ fi
 # Re-execute nixos-rebuild from the Nixpkgs tree.
 # FIXME: get nixos-rebuild from $flake.
 if [[ -z $_NIXOS_REBUILD_REEXEC && -n $canRun && -z $fast && -z $flake ]]; then
-    if p=$(nix-build --no-out-link --expr 'with import <nixpkgs/nixos> {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
+    if p=$(nix-build --no-out-link --expr 'with import ./nixos/. {}; config.system.build.nixos-rebuild' "${extraBuildFlags[@]}"); then
         export _NIXOS_REBUILD_REEXEC=1
         exec "$p/bin/nixos-rebuild" "${origArgs[@]}"
         exit 1

current journal output

building Nix...
building the system configuration...
updating GRUB 2 menu...
activating the configuration...
setting up /etc...
reloading user units for user...
setting up tmpfiles

lmk if you can spot other commands which outputs should be logged

@Artturin
Artturin requested review from dasJ and primeos November 25, 2021 03:36
@Artturin
Artturin force-pushed the rebuildsystemdcat branch 2 times, most recently from c83b8de to 08d65ac Compare November 26, 2021 23:32
@Artturin

Artturin commented Nov 26, 2021 •

Copy link
Copy Markdown
Member Author

@l0b0 hi, you're good at bash so could you review or suggest improvements?

@Artturin

Copy link
Copy Markdown
Member Author

the output of rebuild now looks like this, there's a newline between the building and the switch-to-configuration for some reason, but it may actually be good because now those parts are better separated

building '/nix/store/sdv4ckq5b8kpnih2q10jkan7f11r8h4f-nixos-system-nixos-22.05.git.f01fe92ef7a.drv'...

updating GRUB 2 menu...
NOT restarting the following changed units: [email protected], systemd-fsck@dev-disk-by-uuid-f62675e5-3847-4c11-b504-9cd80cb00e7f.service
activating the configuration...
setting up /etc...
reloading user units for user...
setting up tmpfiles
reloading the following units: dbus.service
restarting the following units: polkit.service

journal output looks like

building Nix...
building the system configuration...
updating GRUB 2 menu...
NOT restarting the following changed units: [email protected], systemd-fsck@dev-disk-by-uuid-f62675e5-3847-4c11-b504-9cd80cb00e7f.service
activating the configuration...
setting up /etc...
reloading user units for user...
setting up tmpfiles
reloading the following units: dbus.service
restarting the following units: polkit.service

user services need to be started manually as they aren't detected
by the activation script at the moment.

output is logged to the systemd journal and can be viewed with <command>journalctl -t nixos-rebuild</command>.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"output" should probably be "Output".

# msg something
msg() {
if command -v systemd-cat &>/dev/null && systemctl &>/dev/null; then
echo -e "$@" | tee >(systemd-cat -t nixos-rebuild)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is -e a good idea here? What if some arbitrary command output contains any backslashes? echo is also problematic since it accepts flags and even accepts different flags based on the shell. For example, if the first argument is -n:

$ messages=('-n' 'foo') && echo "${messages[@]}" | xxd
00000000: 666f 6f                                  foo

Basically, you might want to use printf '%s\n' "$@" instead.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the above printf should use "$*" (single arg) instead of "$@" (multiple args).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It's almost always a bad idea to use $*, as explained in for example What is the difference between $* and $@? and for arg in $*.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd have to test it, but I thought the printf format string had to match the number of arguments?

@l0b0 l0b0 Dec 6, 2021 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printf will reuse the format string repeatedly as long as there are arguments to process, for example:

$ printf '%s\n' 'foo bar' 'baz'
foo bar
baz

Conversely, if there are too few arguments it will fill in an empty string, for example:

$ printf '%s-%s-%s\n' 'foo bar' 'baz'
foo bar-baz-

Relevant to this is that "$@" expands to no arguments if the argument list is empty, but "$*" expands to an argument no matter what:

$ arg_count() {
>     echo $#
> }
$ arg_count "$@"
0
$ arg_count "$*"
1

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the info, TIL.


# msg something
msg() {
if command -v systemd-cat &>/dev/null && systemctl &>/dev/null; then

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

command -v might be better as type. Also, what is the purpose of the systemctl command here? If it's just to check whether that command exists, it's pretty slow. You might want to use type systemctl.

;;
--install-grub)
echo "$0: --install-grub deprecated, use --install-bootloader instead" >&2
msg "$0: --install-grub deprecated, use --install-bootloader instead"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes the semantics, since the message is no longer sent to stderr. You could either keep the redirect here, or use $1 in msg to indicate the target file descriptor, as in printf '%s\n' "$@" >&"$1".

fi
copyToTarget "$pathToConfig"
targetHostCmd nix-env -p "$profile" --set "$pathToConfig"
targetHostCmd nix-env -p "$profile" --set "$pathToConfig" 2>&1 | msgPipe

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we not want to keep stdout and stderr separate for logging purposes? I think the journal treats them differently.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would making a msgErr and msgErrPipe be the best solution?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would work. But at this point maybe an exec which redirects stdout and stderr to separate systemd-cat commands would be a good way to capture all the output rather than only some commands?

@Profpatsch

Copy link
Copy Markdown
Member

Instead of instrumenting the commands and messages printed in the script itself, why don’t we wrap it?

@roberth

roberth commented Mar 23, 2022

Copy link
Copy Markdown
Member

@Profpatsch mostly to avoid logging the build log, which is excessive duplication and not something that can be relied on either, as logs of cached builds won't be duplicated into the system log anyway.

Perhaps a good option is make the script to wrap itself (or redirect its output with exec) after the nix-builds are done and mutation starts.

@Profpatsch

Copy link
Copy Markdown
Member

@Profpatsch mostly to avoid logging the build log, which is excessive duplication and not something that can be relied on either, as logs of cached builds won't be duplicated into the system log anyway.

Ah you mean the nix output is already accessible with nix log … but then we should at least print & log the paths as well.

@stale stale Bot added the 2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md label Sep 21, 2022
@Artturin Artturin closed this Nov 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

2.status: stale https://github.com/NixOS/nixpkgs/blob/master/.github/STALE-BOT.md 6.topic: nixos Issues or PRs affecting NixOS modules, or package usability issues specific to NixOS 8.has: documentation This PR adds or changes documentation 10.rebuild-darwin: 0 This PR does not cause any packages to rebuild on Darwin. 10.rebuild-linux: 1-10 This PR causes between 1 and 10 packages to rebuild on Linux.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants