Nix Generator Functions for Sway

By Calum MacRae

May 10, 2020

A short & sweet post on using the Nix language to generate Sway config 🥝

I don't tend to use Linux on the desktop at the moment and figured it'd be a shame not to share these functions I wrote to generate input & output blocks.

toSwayInputs

Convert a set representing Sway input expressions to valid sway config.

Usage toSwayinputs inputs

Expression

toSwayInputs = inputs:
  concatStringsSep "\n" (
    mapAttrsToList (device: config: ''
    input "${device}" {
      ${concatStringsSep "\n  " (
        mapAttrsToList (p: v: "${p} ${v}") config
      )}
    }
    '') inputs
  );

Example input

inputs = {
  "1:1:AT_Translated_Set_2_keyboard" = {
    xkb_layout = "gb";
    xkb_options = "ctrl:nocaps";
  };

  "1739:0:Synaptics_TM3381-002" = {
    pointer_accel = "0.7";
    tap = "enabled";
    dwt = "enabled";
    natural_scroll = "enabled";
  };
};

toSwayOutputs

Convert a list of sets representing Sway output expressions to valid kanshi config.

Usage toSwayOutputs outputs

Expression

toSwayOutputs = outputs:
  concatStringsSep "\n" (
    forEach outputs (block: ''
      {
        ${concatStringsSep "\n  " (
          mapAttrsToList (display: config:
            "output ${display} ${concatStringsSep " "
              (mapAttrsToList (p: v: "${p} ${v}") config)}"
          ) block)
         }
      }'' )
  );

Example input

outputs = [
  { eDP1 = {};}

  {
    "DP-1" = {
      position = "0,0";
      transform = "270";
    };

    "HDMI-A-2" = { position = "1440,470"; };
    "eDP-1" = { position = "1440,1910"; };
  }
];

Posted on:
May 10, 2020
Length:
1 minute read, 211 words
Tags:
nix linux
See Also:
A Nix overlay for Emacs 27 with the 'emacs-mac' patches
"Cloud-like" Infrastructure at Home - Part 1: LoadBalancers on the Metal
spacebar module in nix-darwin now generally available