nix-community / home-manager

Manage a user environment using Nix [maintainer=@rycee]
https://nix-community.github.io/home-manager/
MIT License
7.06k stars 1.82k forks source link

mkOutOfStoreSymlink doesn't work as expected with a flake setup #2085

Closed folke closed 3 years ago

folke commented 3 years ago

Issue description

I'm currently migrating my dotfiles to Nix using nix-darwin and the home-manager module.

One problem I'm having is that mkOutOfStoreSymlink doesn't seem to work when using flakes.

In the example below, the .config/alacritty directory will link to the flake sources (/nix/store/54vrjl9afxg7ckb5waxy3w521f3mpi3j-hm_alacritty) instead of the original directory in my ~/projects/dot/config/alacritty.

 xdg.configFile."alacritty".source = config.lib.file.mkOutOfStoreSymlink ../../config/alacritty;

Is there any way to do what I want with the flake.nix setup?

cole-h commented 3 years ago

mkOutOfStoreSymlink also supports strings, so you can hardcode it as mkOutOfStoreSymlink "${config.home.homeDirectory}/projects/dot/config/alacritty". If the location ever changes, you'll have to change it then as well.

folke commented 3 years ago

@cole-h that's exactly how I solved it :)

Closing this issue.

andyrichardson commented 3 years ago

@folke when providing a string argument, the link would then point to the source file within your home directory?

I've tried using a string but it's making no difference (NixOS)

xdg.configFile."Code/User/settings.json".source = config.lib.file.mkOutOfStoreSymlink  "${config.home.homeDirectory}/dev/dotfiles/nix/config/settings.json";
ls -l /home/andy/.config/Code/User/settings.json 
lrwxrwxrwx 1 andy users 94 Jun 15 12:39 /home/andy/.config/Code/User/settings.json -> /nix/store/9qqncknpzrfhw21kwa9k634ady5lm0bk-home-manager-files/.config/Code/User/settings.json

Edit: Looks like, when using a string, the link eventually points to the file in the home directory (with correct read/write permissions)

 /home/andy/.config/Code/User/settings.json -> /nix/store/3c2spxdxgcfqw1k01lm27s6n15amx79d-home-manager-files/.config/Code/User/settings.json -> /nix/store/dawm1s7974fa512h95sw4a4w1jymj54a-hm_settings.json -> /home/andy/dev/dotfiles/nix/config/settings.json
folke commented 3 years ago

You need to use a full path with home.file. That works in my case

Lillecarl commented 1 year ago

I scratched my head for awhile about how to get the absolute path of the flake. It ended up being quite dirty. I run my builds through a script. This scripts writes the path to a file within the flake repo, this file is ignored with git update-index --skip-worktree ".flakepath" after an initial commit so that Nix doesn't complain it's not in the repo.

At this point I guess I could just make the script make the symlinks too, but it's pretty nice to have them activated and deactivated through Nix.

Does anyone have a better solution 2023? 😄

nixos-discourse commented 7 months ago

This issue has been mentioned on NixOS Discourse. There might be relevant details there:

https://discourse.nixos.org/t/how-to-manage-dotfiles-with-home-manager/30576/5

amardeep commented 7 months ago

This is what i use currently:

  options = {
    dotfiles = lib.mkOption {
      type = lib.types.path;
      apply = toString;
      default = "${config.home.homeDirectory}/.dotfiles";
      example = "${config.home.homeDirectory}/.dotfiles";
      description = "Location of the dotfiles working copy";
    };
  };

Then, I use this option whenever i want to create a symlink to a file, for eg.

xdg.configFile."wezterm".source = config.lib.file.mkOutOfStoreSymlink "${config.dotfiles}/config/wezterm";

home.file.".p10k.zsh".source = config.lib.file.mkOutOfStoreSymlink "${config.dotfiles}/config/zsh/p10k.zsh";