Wrappers vs. Dotfiles: Difference between revisions

From NixOS Wiki
imported>Fzakaria
Added wrapProgram example
imported>IgorM
m Added to category "Configuration"
 
(3 intermediate revisions by one other user not shown)
Line 14: Line 14:
</syntaxhighlight>
</syntaxhighlight>


Consider also using the simpler ''wrapProgram'' utility
The disadvantage of this way is that it doesn't propagate man pages and other paths from the old derivation.
Please refer to [[Nix_Cookbook#Wrapping_packages]] to possible solutions to retain all outputs.
 
You can use this simple function which takes care of wrapping the script & symlinking
<syntaxhighlight lang="nix">
<syntaxhighlight lang="nix">
{
writeShellScriptBinAndSymlink = name: text: super.symlinkJoin {
  users.users.root.packages = [
    name = name;
    (pkg.wrapProgram ${pkgs.htop}/bin/htop --set HTOPRC ${pkgs.writeText "htoprc" ...}
    paths = [
  ];
      super."${name}"
}
      (super.writeShellScriptBin name text)
    ];
  };
</syntaxhighlight>
</syntaxhighlight>


=== Downside of the Wrapper Approach ===
=== Downside of the Wrapper Approach ===
Line 30: Line 34:
=== Alternatives ===
=== Alternatives ===
* [[Home Manager]] manages dotfiles in the user's home directory
* [[Home Manager]] manages dotfiles in the user's home directory
[[Category:Configuration]]

Latest revision as of 11:09, 27 September 2023

Usually user applications (like editors, etc.) get configured through dotfiles in the user's home directory. An alternative, declarative approach is to create wrappers for application on a per-user basis, like this:

{
  users.users.root.packages = [
    (pkgs.writeScriptBin "htop" ''
      #! ${pkgs.bash}/bin/bash
      export HTOPRC=${pkgs.writeText "htoprc" ...}
      exec ${pkgs.htop}/bin/htop "$@"
    '')
  ];
}

The disadvantage of this way is that it doesn't propagate man pages and other paths from the old derivation. Please refer to Nix_Cookbook#Wrapping_packages to possible solutions to retain all outputs.

You can use this simple function which takes care of wrapping the script & symlinking

 writeShellScriptBinAndSymlink = name: text: super.symlinkJoin {
    name = name;
    paths = [
      super."${name}"
      (super.writeShellScriptBin name text)
    ];
  };

Downside of the Wrapper Approach

  • There might be applications that don't provide means to specify configuration. One could override $HOME, but then there might be applications that require $HOME for other stuff than configuration.
  • Applications cannot write their configuration anymore, e.g. htop will just terminate without error and nothing changed.

Alternatives