Suppose you want to package .vimrc
and .tmux.conf
as a nixpkg that contains a script that inserts them into your home dir.
Let's say you create a directory in /etc/nixos/
called mrkgnao
where you store your .vimrc
, .tmux.conf
and default.nix
files inside:
tree -a /etc/nixos/mrkgnao/
/etc/nixos/mrkgnao/
├── default.nix
└── dotfiles
├── .tmux.conf
└── .vimrc
1 directory, 3 files
To create a package that only contains your dotfiles you would structure your default.nix
like this:
{ stdenv }:
stdenv.mkDerivation rec {
name = "mrkgnao-dotfiles-${version}";
version = "1.1.0";
src = ./dotfiles
dontBuild = true;
installPhase = ''
cp $src $out -R
'';
}
Building this package will create a package that has the following contents:
tree -a /nix/store/vdnma2k6zmslqspdv18q3g3wssq6n7dp-mrkgnao-dotfiles-1.1.0
/nix/store/vdnma2k6zmslqspdv18q3g3wssq6n7dp-mrkgnao-dotfiles-1.1.0
├── .tmux.conf
└── .vimrc
0 directories, 2 files
Then you can just add a script that copies the dotfiles from the package to the users home dir:
/etc/nixos/mrkgnao/dotfiles/mrkgnao-insert-dotfiles
#!/usr/bin/env bash
pkg_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cp $pkg_dir/.vimrc ~/.vimrc
cp $pkg_dir/.tmux.conf ~/.tmux.conf
echo "-- mrkgnao dotfiles inserted!"
Then when you reinstall your package it will have the following contents:
tree -a /nix/store/rwzjc4cvrbflyh8v2k6l6j8a6sy4vi1y-mrkgnao-dotfiles-1.1.0
/nix/store/rwzjc4cvrbflyh8v2k6l6j8a6sy4vi1y-mrkgnao-dotfiles-1.1.0
├── mrkgnao-dotfiles-insert
├── .tmux.conf
└── .vimrc
To then install that package as a package available for everyone on the system you just import it in your /etc/nixos/configuration.nix
file and include the package inside environment.systemPackages
:
{ config, pkgs, ... }:
let
mrkgnaoDotfiles = callPackage ./mrkgnao/default.nix {};
in
{
environment.systemPackages = [ mrkgnaoDotfiles ];
}
Then after you have performed nixos-rebuild switch
all users have the command mrkgnao-insert-dotfiles
available:
$ mrkgnao-insert-dotfiles
-- mrkgnao dotfiles inserted!
Hey @mrkgnao
Here's the example of handling dotfiles in NixOS that I wanted to show you in the #nixos IRC channel today (my IRC nick is rmrfroot).