You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This is more of a would-you-be-interested-in-this-addition type of question, rather than an issue.
I did not directly want to embed the configuration of all of my dotfiles file in home-manager, and wanted to include every file in my existing git repo when switching to the new config, without hassle. So I've wrote a function that made migrating to home-manager easier by mapping the files from an existing git-repo containing dotfiles to the required home-manager attributes.
I've included the code below.
home.nix
let
pkgsUnstable = import <nixpkgs-unstable>{};
....
username = "pimvh";
homeDirectory = "/home/${username}";
dotFiles = import ./dotfiles.nix {
lib = lib;
homeDirectory = homeDirectory;
};
...
in {
# Home Manager needs a bit of information about you and the paths it should
# manage.
home.username = username;
home.homeDirectory = homeDirectory;
home.file = dotFiles.all.home;
xdg.configFile = dotFiles.all.xdgConfig;
./dotfiles.nix
# use the relative path in dotfiles GitHub repository (./dotfiles, submodule from home-manager repo)
# to put files in the correct place.
{ lib, homeDirectory, ... }:
let
generateDotFilesPaths = rootPath: (
let
fileList = lib.filesystem.listFilesRecursive rootPath;
# filter the files which contain `.git` after removing the prefix of the fileList
relativePaths = builtins.filter (x: ! lib.hasInfix ".git" x) (map (x: lib.removePrefix rootPath x) fileList);
# build a function that to generate the required structure.
generateFileAttrs = val: {
"${val}".source = "${rootPath}/${val}";
};
# map the list of files into that structure.
# differentiate home files (anything in ~), and files stored under .config
homeFilesList = builtins.filter (x: !lib.hasInfix ".config" x) relativePaths;
xdgConfigFilesList = builtins.filter (x: lib.hasInfix ".config" x) relativePaths;
# merge the elements of the list into a single map
homeFiles = lib.attrsets.mergeAttrsList (map generateFileAttrs homeFilesList);
xdgConfigFiles = lib.attrsets.mergeAttrsList (map generateFileAttrs xdgConfigFilesList);
in
{
home = homeFiles;
xdgConfig = xdgConfigFiles;
}
);
in {
# make a function that takes in a path to the root
# string -> attrset[string : attrset]
all = generateDotFilesPaths "${homeDirectory}/.config/home-manager/dotfiles";
}
As your repository already has good exposure I think it would help others out. Let me know if you would like me to contribute it.
The text was updated successfully, but these errors were encountered:
pimvh
changed the title
Function to iterate over files in git repository
Adding a function to iterate over files in git repository?
Aug 28, 2023
Hi! Thank you for this excellent resource.
This is more of a would-you-be-interested-in-this-addition type of question, rather than an issue.
I did not directly want to embed the configuration of all of my dotfiles file in home-manager, and wanted to include every file in my existing git repo when switching to the new config, without hassle. So I've wrote a function that made migrating to home-manager easier by mapping the files from an existing git-repo containing dotfiles to the required home-manager attributes.
I've included the code below.
home.nix
./dotfiles.nix
As your repository already has good exposure I think it would help others out. Let me know if you would like me to contribute it.
The text was updated successfully, but these errors were encountered: