Files
NixOsConfig/modules/desktop/noctalia.nix
T
faumaray 079cceb554 update
Signed-off-by: faumaray <faumaray@gmail.com>
2026-10-29 16:02:14 +03:00

291 lines
13 KiB
Nix

# Noctalia v5 shell (bar, launcher, notifications, lock screen, wallpaper),
# both halves of one feature:
# NixOS the package and the services its panels talk to
# (NetworkManager, Bluetooth, UPower, power profiles). The user
# service is started by home-manager, so
# `programs.noctalia.systemd` stays off here.
# home-manager the user service (started once Hyprland answers) and the shell
# settings.
# The NixOS half adds the home-manager half to the owner, so a host imports
# `nixos.noctalia` once.
#
# Needs the hyprland aspect (desktop/hyprland, with UWSM): the service waits for
# Hyprland's request socket (hyprctl), and apps and terminals start through
# uwsm-app. The NixOS half asserts it.
#
# The modules come from the noctalia flake. home-manager ships its own
# `programs.noctalia` too (modules/programs/noctalia), and nixpkgs has one for
# NixOS; the flake's modules list those in `disabledModules`, so importing them
# here replaces the others and nothing is declared twice. They are imported
# only in this file (a module value imported twice declares its options twice).
#
# Colours: Stylix's `noctalia` target (theme/stylix.nix) writes the custom
# palette `stylix` and `theme.*`; the shell font comes from the same target
# (overridden to Monaspace Krypton there). So no `theme` and no `font_family`
# here. Settings are validated at build time by `noctalia config validate`, so
# only keys from the upstream schema may appear. Anything changed in the
# settings window at runtime is stored separately and overrides the file.
{ config, inputs, ... }:
let
inherit (config.meta) owner;
hmNoctalia = config.flake.modules.homeManager.noctalia;
in
{
flake.modules.nixos.noctalia =
{ config, pkgs, ... }:
{
imports = [ inputs.noctalia.nixosModules.default ];
assertions = [
{
assertion = config.programs.hyprland.enable && config.programs.hyprland.withUWSM;
message = ''
The Noctalia service (desktop/noctalia.nix) waits for Hyprland and
starts apps through uwsm-app, so it needs the hyprland aspect
(desktop/hyprland) with programs.hyprland.withUWSM.
'';
}
];
home-manager.users.${owner.name}.imports = [ hmNoctalia ];
programs.noctalia = {
enable = true;
package = pkgs.noctalia;
# NetworkManager, Bluetooth, UPower and power-profiles-daemon, which the
# shell's panels talk to.
recommendedServices.enable = true;
};
};
flake.modules.homeManager.noctalia =
{
config,
lib,
pkgs,
osConfig,
...
}:
let
cfg = config.programs.noctalia;
inherit (osConfig.stylix) image;
# Starts apps as scopes of the graphical session (see launch_apps_*
# below). The same launcher the Hyprland binds use.
uwsmApp = lib.getExe' osConfig.programs.uwsm.package "uwsm-app";
# Noctalia asks Hyprland once, at startup, whether the config is Lua
# (`j/status`, 500 ms timeout, no retry) and sends a workspace click as
# hl.dsp.focus or as the legacy `dispatch workspace` accordingly; a Lua
# config rejects the legacy form. graphical-session.target comes up before
# the compositor answers on its request socket, so at login that request
# can time out (the journal shows a 0.5 s pause right after the
# RLIMIT_NOFILE line), Noctalia keeps "not Lua", and the workspace buttons
# in the bar do nothing until the shell is restarted. So start it only once
# Hyprland has answered the status request (a JSON object with
# `configProvider`; what it says is Noctalia's business, it only needs the
# request to succeed, so a non-Lua config does not stall the start) and has
# workspaces (the workspace list also decides Noctalia's IPC schema). After
# 15 s start anyway, a late bar beats none.
#
# XDG_SESSION_ID: a user service is not part of the login session, so
# logind cannot find the session by PID (NoSessionForPID). Noctalia then
# loses the lock-session handling and the brightness lookup. It tries
# XDG_SESSION_ID first, so export the user's graphical session.
#
# exec, so Restart=on-failure sees Noctalia's own exit status. Nothing in
# the wait or the lookup may fail the unit.
session = pkgs.writeShellApplication {
name = "noctalia-session";
runtimeInputs = [ pkgs.coreutils ];
text = ''
hyprctl=${lib.getExe' osConfig.programs.hyprland.package "hyprctl"}
jq=${lib.getExe pkgs.jq}
# Both probes get the 0.5 s Noctalia's own request has.
ready() {
[ -n "''${HYPRLAND_INSTANCE_SIGNATURE:-}" ] &&
timeout 0.5 "$hyprctl" -j status | "$jq" -e 'has("configProvider")' >/dev/null &&
timeout 0.5 "$hyprctl" -j workspaces | "$jq" -e 'length > 0' >/dev/null
}
deadline=$((SECONDS + 15))
until ready 2>/dev/null; do
if [ "$SECONDS" -ge "$deadline" ]; then
echo "Hyprland not ready after 15 s, starting Noctalia anyway" >&2
break
fi
sleep 0.2
done
session=$(${lib.getExe' osConfig.systemd.package "loginctl"} show-user "$(id -u)" -p Display --value 2>/dev/null || true)
if [ -n "$session" ]; then
export XDG_SESSION_ID="$session"
fi
exec ${lib.getExe cfg.package}
'';
};
# Stylix's wallpaper is a store path that changes with every new image,
# and Noctalia keeps the path it was given in its runtime state. A fixed
# link in $HOME survives that; the file behind it is replaced on switch.
wallpaperExt = lib.optionalString (builtins.match ".*(\\.[A-Za-z0-9]+)" image != null) (
lib.head (builtins.match ".*(\\.[A-Za-z0-9]+)" image)
);
wallpaperFile = "wallpapers/wallpaper${wallpaperExt}";
wallpaperLink = "${config.xdg.dataHome}/${wallpaperFile}";
# Noctalia stores the wallpaper it was given (or picked in its own panel)
# in its runtime state, ~/.local/state/noctalia/settings.toml, which is
# applied after the declarative settings and wins over them. A path from
# the Nix store (the bundled image of a first run, an older Stylix image)
# stays in there after garbage collection deletes the file, and the
# shell then shows no wallpaper whatever `wallpaper.default.path` says.
# So once Noctalia answers on its IPC socket, look at what it reports
# and, if that file is gone, point it at the stable link instead.
#
# `wallpaper-get` prints the default path; `wallpaper-get <connector>` the
# effective one of a monitor (its own entry, else the default), and fails
# for a monitor Noctalia does not know. `wallpaper-set <path>` sets the
# default and every connected monitor (there is no call for the default
# alone), `wallpaper-set <connector> <path>` one monitor, both persisted
# to the runtime state. A missing default is repaired for all monitors,
# otherwise only the monitors whose own path is gone.
#
# Run as ExecStartPost, so it may never fail the unit: it gives up quietly
# when Noctalia does not answer within 10 s (plus one 2 s call), when the
# link is not there yet, or when a call fails.
healWallpaper = pkgs.writeShellApplication {
name = "noctalia-heal-wallpaper";
runtimeInputs = [
pkgs.coreutils
pkgs.jq
];
text = ''
noctalia=${lib.getExe cfg.package}
hyprctl=${lib.getExe' osConfig.programs.hyprland.package "hyprctl"}
link=${lib.escapeShellArg wallpaperLink}
# Nothing to point Noctalia at before home-manager has linked the image.
[ -f "$link" ] || exit 0
msg() { timeout 2 "$noctalia" msg "$@" 2>/dev/null; }
# A regular file (a symlink counts when its target is one), or the solid
# colour Noctalia also takes as a wallpaper.
usable() { [ -f "$1" ] || [[ "$1" == color:* ]]; }
# Rc 1 means the socket is not there yet or the wallpaper service is not
# initialised yet; either way try again.
deadline=$((SECONDS + 10))
until default=$(msg wallpaper-get); do
if [ "$SECONDS" -ge "$deadline" ]; then
echo "Noctalia did not answer within 10 s, wallpaper not checked" >&2
exit 0
fi
sleep 0.2
done
if ! usable "$default"; then
echo "Noctalia's wallpaper \"$default\" is gone, using $link" >&2
reply=$(msg wallpaper-set "$link") || echo "wallpaper-set failed: $reply" >&2
exit 0
fi
mapfile -t monitors < <(timeout 1 "$hyprctl" -j monitors 2>/dev/null | jq -r '.[].name' 2>/dev/null || true)
for monitor in "''${monitors[@]}"; do
path=$(msg wallpaper-get "$monitor") || continue
if ! usable "$path"; then
echo "Noctalia's wallpaper \"$path\" on $monitor is gone, using $link" >&2
reply=$(msg wallpaper-set "$monitor" "$link") || echo "wallpaper-set $monitor failed: $reply" >&2
fi
done
'';
};
# Terminal=true launcher entries: Noctalia runs `$TERMINAL -e sh -lc
# <command>` (the command already wrapped in uwsm-app, see
# launch_apps_custom_command) and starts that with a plain fork, so the
# terminal would live in noctalia.service's cgroup and die whenever the
# shell restarts, on every switch that changes it. This is the $TERMINAL
# Noctalia gets (Service.Environment below): it takes that call and
# starts WezTerm through uwsm-app, as a scope of its own like the Hyprland
# binds do. The terminal is the one of the binds (nixVars.terminal in
# hyprland/default.nix), started by name and resolved by uwsm.
#
# Noctalia's `-e` is dropped and WezTerm's own `start --` put in its
# place: WezTerm knows `-e` only as another name of the `start`
# subcommand, and the `--` keeps `-lc` from being read as a WezTerm
# option. uwsm-terminal* do not fit: they run `uwsm app -T`, which builds
# the call from the terminal's desktop entry (`wezterm start --cwd .`)
# plus `-e`, and `wezterm start ... -e` is not a WezTerm option.
terminal = pkgs.writeShellApplication {
name = "noctalia-terminal";
text = ''
if [ "''${1:-}" = "-e" ]; then
shift
fi
if [ "$#" -gt 0 ]; then
set -- -- "$@"
fi
exec ${uwsmApp} -- wezterm start "$@"
'';
};
in
{
imports = [ inputs.noctalia.homeModules.default ];
config = lib.mkMerge [
{
programs.noctalia = {
enable = true;
# A user service instead of hl.exec_cmd, so it restarts on failure and
# follows graphical-session.target (managed by UWSM).
systemd.enable = true;
settings.shell = {
telemetry_enabled = false;
# No other polkit agent is started in the session.
polkit_agent = true;
# Apps from the launcher, dock and taskbar start through uwsm-app, as
# scopes in app-graphical.slice. UWSM stops that slice before the
# compositor, so apps close cleanly at logout (Firefox saves its
# session), and restarting the shell does not kill them. Noctalia's own
# systemd launcher would put them in app.slice, which is not ordered
# before the compositor, and it overrides the custom command, so it
# stays off.
launch_apps_as_systemd_services = false;
launch_apps_custom_command = "${uwsmApp} -- $CMD";
};
};
systemd.user.services.noctalia.Service = {
ExecStart = lib.mkForce (lib.getExe session);
# The terminal for Terminal=true entries (see `terminal` above).
Environment = [ "TERMINAL=${lib.getExe terminal}" ];
};
}
(lib.mkIf (image != null) {
xdg.dataFile.${wallpaperFile}.source = image;
# Stylix's noctalia target writes the same key with the store path.
programs.noctalia.settings.wallpaper.default.path = lib.mkForce wallpaperLink;
# Another image, or the same path with new contents: reload the shell.
systemd.user.services.noctalia.Unit.X-Restart-Triggers = [ "${image}" ];
# Repairs a wallpaper path in Noctalia's runtime state that no longer
# exists (see `healWallpaper` above). The `-` keeps a failure from
# failing the unit; the script does not fail anyway.
systemd.user.services.noctalia.Service.ExecStartPost = "-${lib.getExe healWallpaper}";
})
];
};
}