How to load Config files for i3 using nix

Basically, How can I load config files for i3. Lets say I have a github repository that contains config files for i3 and I want to fetch that repository and load up all my config files. Because as far as I know in nix applications are sandboxed in nix/store and using derivations we cannot access files outside of it when building.
I have 2 questions regarding the same thing -

  1. lets say I have config files for the application then how can I load them.
  2. If I have a shell scripts for loading up those config files.

currently I use this basic module to load up i3…

{ config, pkgs, callPackage, ... }: 

{

  environment.pathsToLink = [ "/libexec" ]; 

  services.xserver = {
    enable = true;

    desktopManager = {
      xterm.enable = false;
    };
  
      displayManager = {
        defaultSession = "none+i3";
    };
    
    windowManager.i3 = {
      enable = true;
      extraPackages = with pkgs; [
        dmenu 
        i3status 
        i3lock
        i3blocks 
        rofi
     ];
    };
  };
 
}

They are not, builds are sandboxed, there is no runtime sandboxing.

There are many solutions to your posited problem. You can do anything from downloading configuration files with fetchFromGitHub with a custom derivation at build time to dynamically fetching your config at runtime with a systemd unit.

Personally, I’d suggest putting your configuration files in the same repository as your NixOS configuration, i.e. create dotfiles/i3 with the contents of your former i3-specific repo, and doing something like this:

{
  # <snip>
  windowManager.i3 = {
    enable = true;
    extraPackages = with pkgs; [
      dmenu 
      i3status 
      i3lock
      i3blocks 
      rofi
    ];

    configFile = ./dotfiles/i3/config;
  };
}

Having monorepos, especially for dotfiles, just tends to save on cross-repo maintenance effort. Changes in dotfiles typically require changes in the nix code as well, so keeping them close together just makes your commits make more sense.

YMMV depending on the complexity of your configuration, though. You might need other alternatives if it’s not just a single file. home-manager would be able to symlink a whole directory to $HOME, for example, which may be handy for more complex configs:

{
  # <snip>
  windowManager.i3 = {
    enable = true;
    extraPackages = with pkgs; [
      dmenu 
      i3status 
      i3lock
      i3blocks 
      rofi
    ];
  };

  home-manager.users.sandptel = {pkgs, ...}: {
    # The NixOS module will by default take configuration from `~/.i3`
    home.file.".i3" = {
      source = ./dotfiles/i3;
      onChange = ''
        ${pkgs.i3}/bin/i3-msg reload
      '';
    };
  };
}

… but now we’re getting into territory where you might need to think about how you build up the structure of your NixOS configuration, whether to fully commit to nix-based config and rewrite your i3 config with home-manager, …

Basically, you have many options, and whatever you want to do at build- or runtime is probably possible with some dedication.

1 Like