Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding a function to iterate over files in git repository? #46

Open
pimvh opened this issue Aug 28, 2023 · 0 comments
Open

Adding a function to iterate over files in git repository? #46

pimvh opened this issue Aug 28, 2023 · 0 comments
Assignees

Comments

@pimvh
Copy link

pimvh commented 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

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.

@pimvh 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
@Misterio77 Misterio77 self-assigned this Nov 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants