Skip to content

Instantly share code, notes, and snippets.

@rzetterberg
Last active January 6, 2017 19:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rzetterberg/7cbd2d869a995d29f20d45ce28bdd680 to your computer and use it in GitHub Desktop.
Save rzetterberg/7cbd2d869a995d29f20d45ce28bdd680 to your computer and use it in GitHub Desktop.

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!
@rzetterberg
Copy link
Author

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment