/dconf2nix

:feet: Convert Dconf files (e.g. Gnome Shell) to Nix, as expected by Home Manager

Primary LanguageNixApache License 2.0Apache-2.0

dconf2nix

CI Status

A convenient converter of DConf files to Nix, as expected by Home Manager's dconf settings. So you can Nixify your Gnome Shell configuration 😉


Benchmarks

Take it with a grain of salt but on my machine it takes an average of 7.1ms to process a 349 lines configuration and generate a Nix file with 433 lines.

benchmarks

Introduction

Given the following dconf settings:

[org/gnome/desktop/peripherals/mouse]
natural-scroll=false
speed=-0.5

[org/gnome/desktop/peripherals/touchpad]
tap-to-click=false
two-finger-scrolling-enabled=true

[org/gnome/desktop/input-sources]
current=uint32 0
sources=[('xkb', 'us')]
xkb-options=[' terminate:ctrl_alt_bksp ', ' lv3:ralt_switch ', ' caps:ctrl_modifier ']

[org/gnome/desktop/screensaver]
picture-uri=' file:///home/gvolpe/Pictures/nixos.png '

You will get the following output when running dconf2nix:

{ lib, ... }:

let
  mkTuple = lib.hm.gvariant.mkTuple;
in
{
  dconf.settings = {
    "org/gnome/desktop/peripherals/mouse" = {
      natural-scroll = false;
      speed = -0.5;
    };

    "org/gnome/desktop/peripherals/touchpad" = {
      tap-to-click = false;
      two-finger-scrolling-enabled = true;
    };

    "org/gnome/desktop/input-sources" = {
      current = "uint32 0";
      sources = [ (mkTuple [ "xkb" "us" ]) ];
      xkb-options = [ "terminate:ctrl_alt_bksp" "lv3:ralt_switch" "caps:ctrl_modifier" ];
    };

    "org/gnome/desktop/screensaver" = {
      picture-uri = "file:///home/gvolpe/Pictures/nixos.png";
    };
  };

}

It makes use of the Home Manager's dconf.settings key.

You can make changes in the UI and create a dump of your dconf file at any time, which you can Nixify so Home Manager can restore the next time you run home-manager switch. To create a dump, run the following command:

dconf dump / > dconf.settings

Run

The easiest way is to pipe the standard input to dconf2nix and expect the result in the standard output:

dconf dump / | dconf2nix > dconf.nix

If you have an input file instead, you can run the following command:

dconf2nix -i data/dconf.settings -o output/dconf.nix

Type --help for some more information.

dconf2nix - Nixify dconf configuration files

Usage: dconf2nix [-v|--version]
                 [[-r|--root ARG] [-t|--timeout ARG] [--verbose] |
                   (-i|--input ARG) (-o|--output ARG) [-r|--root ARG]
                   [-t|--timeout ARG] [--verbose]]
  Convert a dconf file into a Nix file, as expected by Home Manager.

Available options:
  -h,--help                Show this help text
  -v,--version             Show the current version
  -r,--root ARG            Custom root path. e.g.: system/locale/
  -t,--timeout ARG         Timeout in seconds for the conversion
                           process (default: 5)
  --verbose                Verbose mode (debug)
  -i,--input ARG           Path to the dconf file (input)
  -o,--output ARG          Path to the Nix output file (to be created)
  -r,--root ARG            Custom root path. e.g.: system/locale/
  -t,--timeout ARG         Timeout in seconds for the conversion
                           process (default: 5)
  --verbose                Verbose mode (debug)

Custom root

By default, dconf2nix expects the root to be /. If you want to create a dump of a custom root, you can use the --root flag. For example:

dconf dump /system/locale/ | dconf2nix --root system/locale > dconf.nix

This will generate an output similar to the one below.

{
  dconf.settings = {
    "system/locale" = {
      region = "en_US.UTF-8";
    };

  };
}

Supported types

For now, only types supported by Home Manager as specified here are supported. If there's enough interest, we might be able to work on supporting the full specification.

Due to the lack of support, dconf2nix parses dictionaries and list of variants as simple strings to avoid failing to parse a file and retain most of the information.

Gnome Shell configuration

Once you have your dconf.nix, you can import it via Home Manager.

{
  programs.home-manager.enable = true;

  imports = [
    ./dconf.nix
  ];
}

If you are using the Home Manager module for NixOS you can import it like so:

{
  home-manager.users.joe = { pkgs, ... }: {
    imports = [ ./dconf.nix ];
	# ...
  };
}

Installation

The simplest way is to install it via nix-env.

nix-env -i dconf2nix

Or if you want to pull the latest master.

nix-env -i -f https://github.com/gvolpe/dconf2nix/archive/master.tar.gz

You could also use Cachix to reduce the installation time.

Alternatively, here's an overlay for the binary you can use to avoid compiling it (only for Linux-x86-64).

self: super:

rec {
  dconf2nix = super.dconf2nix.overrideAttrs (
    old: rec {
      version = "v0.0.6";

      src = builtins.fetchurl {
        url    = "https://github.com/gvolpe/dconf2nix/releases/download/${version}/dconf2nix-linux-x86-64";
        sha256 = "1bh78hfgy4wnfdq184ck5yw72szllzl5sm7a3a4y46byq0xxklcd";
      };

      phases = ["installPhase" "patchPhase"];

      installPhase = ''
        mkdir -p $out/bin
        cp $src $out/bin/dconf2nix
        chmod +x $out/bin/dconf2nix
      '';
    }
  );
}

Have a look at the latest releases in case the README file gets outdated.

Troubleshooting

error

The default timeout is of 5 seconds. You can see it by running dconf2nix --help.

To find which section caused the error you can download d2n_util.sh (made by Broccoli):

curl https://raw.githubusercontent.com/broccoli5/Scripts/main/d2n_util.sh > d2n_util.sh && chmod +x d2n_util.sh

You can then run: dconf dump / | ./d2n_util.sh -t to create the sections and automaticaly test them. When creating a issue include both, the sections which failed the test and the errors from "d2n.log". For more options run ./d2n_util.sh -h

Do also consider the caveats mentioned above in the Supported Types section.

Development

To compile and run the tests locally.

cabal new-configure
cabal new-run dconf2nix-tests

To generate the static binary.

cabal new-configure --disable-executable-dynamic --ghc-option=-optl=-static --ghc-option=-optl=-pthread
nix-build

If everything goes well, the binary should be under result/bin/.