You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NixOS Home Manager配置Sway时bindswitches选项不存在报错

问题:NixOS 25.05 + Home Manager配置Sway时bindswitches选项不存在错误

我正在NixOS上通过Home Manager配置Sway窗口管理器,使用home-manager v25.05与nixos v25.05版本,配置代码如下:

{ ... }:
let
  laptop = "eDP-1";
in
{
  wayland.windowManager.sway = {
    enable = true;
    wrapperFeatures.gtk = true;
    package = null;
    config = rec {
      bindswitches = {
        "lid:on" = {
          reload = true;
          locked = true;
          action = "output ${laptop} disable";
        };
        "lid:off" = {
          reload = true;
          locked = true;
          action = "output ${laptop} enable";
        };
      };
      assigns = {
        "1: web" = [{ class = "^Firefox$"; }];
        "0: ide" = [{ class = "jetbrains-rustrover"; }];
      };
      defaultWorkspace = "workspace number 1";
      modifier = "Mod4";
      terminal = "kitty";
      window.titlebar = false;
      bars = [{ command = "waybar"; }];
      startup = [
        { command = "rust-rover"; }
        { command = "firefox"; }
        { command = "fuzzel"; }
      ];
      keybindings = {
        "${modifier}+0" = "workspace number 10";
        "${modifier}+1" = "workspace number 1";
        "${modifier}+2" = "workspace number 2";
        "${modifier}+3" = "workspace number 3";
        "${modifier}+4" = "workspace number 4";
        "${modifier}+5" = "workspace number 5";
        "${modifier}+6" = "workspace number 6";
        "${modifier}+7" = "workspace number 7";
        "${modifier}+8" = "workspace number 8";
        "${modifier}+9" = "workspace number 9";
        "${modifier}+Down" = "focus down";
        "${modifier}+Left" = "focus left";
        "${modifier}+c" = "exec kitty";
        "${modifier}+Right" = "focus right";
        "${modifier}+Shift+0" = "move container to workspace number 10";
        "${modifier}+Shift+1" = "move container to workspace number 1";
        "${modifier}+Shift+2" = "move container to workspace number 2";
        "${modifier}+Shift+3" = "move container to workspace number 3";
        "${modifier}+Shift+4" = "move container to workspace number 4";
        "${modifier}+Shift+5" = "move container to workspace number 5";
        "${modifier}+Shift+6" = "move container to workspace number 6";
        "${modifier}+Shift+7" = "move container to workspace number 7";
        "${modifier}+Shift+8" = "move container to workspace number 8";
        "${modifier}+Shift+9" = "move container to workspace number 9";
        "${modifier}+Ctrl+Down" = "move down";
        "${modifier}+Ctrl+Left" = "move left";
        "${modifier}+Ctrl+Right" = "move right";
        "${modifier}+Ctrl+Up" = "move up";
        "${modifier}+Shift+c" = "reload";
        "${modifier}+Shift+e" = "exec swaynag -t warning -m 'You pressed the exit shortcut. Do you really want to exit sway? This will end your Wayland session.' -b 'Yes, exit sway' 'swaymsg exit'";
        "${modifier}+Shift+h" = "move left";
        "${modifier}+Shift+j" = "move down";
        "${modifier}+Shift+k" = "move up";
        "${modifier}+Shift+l" = "move right";
        "${modifier}+Shift+minus" = "move scratchpad";
        "${modifier}+Shift+q" = "kill";
        "${modifier}+Shift+space" = "floating toggle";
        "${modifier}+Up" = "focus up";
        "${modifier}+a" = "focus parent";
        "${modifier}+b" = "splith";
        "${modifier}+e" = "layout toggle split";
        "${modifier}+f" = "fullscreen toggle";
        "${modifier}+h" = "focus left";
        "${modifier}+j" = "focus down";
        "${modifier}+k" ="focus up";
        "${modifier}+l" = "focus right";
        "${modifier}+minus" = "scratchpad show";
        "${modifier}+r" = "mode resize";
#        "${modifier}+s" = "layout stacking";
        "${modifier}+space" = "focus mode_toggle";
        "${modifier}+v" = "splitv";
        "${modifier}+w" = "layout tabbed";
        "${modifier}+d" = "exec fuzzel";
        "${modifier}+Shift+Left" = "workspace prev";
        "${modifier}+Shift+Right" = "workspace next";
      };
      input = {
        "type:touchpad" = {
          dwt = "enabled";
          tap = "enabled";
          natural_scroll = "disabled";
          middle_emulation = "enabled";
        };
      };
    };
  };
}

执行sudo nixos-rebuild switch命令时出现如下错误:

error: The option `home-manager.users.username.wayland.windowManager.sway.config.bindswitches' does not exist. Definition values:
       - In `/nix/store/g9863dnbpnfa7vz8c1s1idwbg7c5j0bb-source/flake.nix':
           {
             "lid:off" = {
               action = "output eDP-1 enable";
               locked = true;
               reload = true;
           ...

该bindswitches配置直接取自Home Manager Sway官方文档,但系统提示该选项不存在,想了解操作中的错误之处。


解决方案

错误原因是Home Manager 25.05版本对Sway的配置结构做了调整,bindswitches不再是config的子选项,而是直接作为sway配置块的顶层属性。你之前把它放在了config内部,导致系统识别不到这个选项。

修正后的核心配置片段如下(其余代码保持不变):

{ ... }:
let
  laptop = "eDP-1";
in
{
  wayland.windowManager.sway = {
    enable = true;
    wrapperFeatures.gtk = true;
    package = null;
    # 将bindswitches移到config外面,作为sway的顶层配置项
    bindswitches = {
      "lid:on" = {
        reload = true;
        locked = true;
        action = "output ${laptop} disable";
      };
      "lid:off" = {
        reload = true;
        locked = true;
        action = "output ${laptop} enable";
      };
    };
    config = rec {
      assigns = {
        "1: web" = [{ class = "^Firefox$"; }];
        "0: ide" = [{ class = "jetbrains-rustrover"; }];
      };
      # 其余config内的配置保持不变...
    };
  };
}

调整后重新执行sudo nixos-rebuild switch即可完成配置。

内容的提问来源于stack exchange,提问作者Marcus Ruddick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 00:35:54