Lehmanator / nix-configs

Personal Nix / NixOS configs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`xdg`: Wrap binaries that don't follow XDG Base Dirs spec.

Lehmanator opened this issue · comments

Problem: Many programs don't support the XDG Base Directory specification. These programs often pollute a user's $HOME directory or other directories with unwanted files that would be better placed in the appropriate XDG directories.

Solution: Many of these programs can be configured to use alternate files/directories by running the program with CLI options passed, environment variables set, or config files/options set. Wrap these programs with whatever is necessary to get these programs closer to the XDG Base Directory spec.

List of Programs

Possibilities

  • Create wrapped packages with included env vars, cmdline options, and/or config files.
  • Overlay original packages w/ wrapped version, so no other NixOS / home-manager config is necessary.
  • Create new packages and set NixOS / home-manager options like program.<name>.package = pkgs.<name>-xdg-compliant (or whatever name) to use the wrapped versions while keeping the original programs.

Docs

Nix Libs / Examples

Trivial Builders

  • pkgs.wrapShellScriptBin
  • pkgs.wrapShellScript
  • pkgs.runCommand
  • pkgs.writeText
  • pkgs.writeTextFile

Functions available in pkgs.stdenv

Example Snippets

(pkgs.writeScriptBin "htop" ''
  #! ${pkgs.bash}/bin/bash
  export HTOPRC=${pkgs.writeText "htoprc" ...}
  exec ${pkgs.htop}/bin/htop "$@"
'')
writeShellScriptBinAndSymlink = name: text: super.symlinkJoin {
  name = name;
  paths = [
    super."${name}"
    (super.writeShellScriptBin name text)
  ];
};
pkgs.writeShellScriptBin "hello" ''
  # Call hello with a traditional greeting 
  exec ${pkgs.hello}/bin/hello -t
''
pkgs.runCommand "hello" {
  buildInputs = [ pkgs.makeWrapper ];
} ''
  mkdir $out
  # Link every top-level folder from pkgs.hello to our new target
  ln -s ${pkgs.hello}/* $out
  # Except the bin folder
  rm $out/bin
  mkdir $out/bin
  # We create the bin folder ourselves and link every binary in it
  ln -s ${pkgs.hello}/bin/* $out/bin
  # Except the hello binary
  rm $out/bin/hello
  # Because we create this ourself, by creating a wrapper
  makeWrapper ${pkgs.hello}/bin/hello $out/bin/hello \
    --add-flags "-t"
''
pkgs.symlinkJoin {
  name = "hello";
  paths = [ pkgs.hello ];
  buildInputs = [ pkgs.makeWrapper ];
  postBuild = ''
    wrapProgram $out/bin/hello \
      --add-flags "-t"
  '';
}