nix-community / impermanence

Modules to help you handle persistent state on systems with ephemeral root storage [maintainer=@talyz]
MIT License
1.09k stars 80 forks source link

How to use impermanence + home-manager module with flake #92

Closed rapenne-s closed 2 years ago

rapenne-s commented 2 years ago

Hi,

I'm trying to switch my configuration by using a flake, everything work so far except impermanence and its home-manager module.

I get this error

[root@t470:/etc/nixos]# nixos-rebuild build --flake '.#t470'
building the system configuration...
warning: Git tree '/etc/nixos' is dirty
error: The option `home' does not exist. Definition values:
       - In `/nix/store/j4n45y0r4ybms9vz64wisbffq7hbf78q-source/flake.nix':
           {
             persistence = {
               "/nix/home/solene" = {
                 allowOther = true;
                 directories = [
           ...

when using the following configuration

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";

    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    impermanence = {
      url = "github:nix-community/impermanence";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, impermanence, home-manager, ... }@inputs:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs { inherit system; config = { allowUnfree = true; }; };
      lib = nixpkgs.lib;

    in
    {

      # begin nixosConfigurations
      nixosConfigurations = {
        t470 = lib.nixosSystem {
          inherit system;
          inherit lib;
          specialArgs = { inherit inputs; };

          # begin modules
          modules = [
            (import ./configuration.nix)

            # begin home-manager
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.solene = {
                home.username = "solene";
                home.homeDirectory = "/home/solene";
              };
            } # end home-manager

            # begin impermanence
            impermanence.nixosModules.impermanence
            impermanence.nixosModules
            {
              environment.persistence."/nix/" = {
                directories = [
                  "/etc/nixos"
                  "/nix/var/nix"
                  "/var/lib"
                  "/var/log"
                  "/tmp"
                ];
              };
              environment.etc."machine-id".source = "/nix/etc/machine-id";

              home.persistence."/nix/home/solene" = {
                allowOther = true;
                removePrefixDirectory = false;
                directories = [
                  ".gnupg"
                  ".ssh"
                ];
                files = [
                  ".gitconfig"
                ];

              };
            } # end impermanence
          ]; # end modules
        }; # end nixosConfigurations
      };
    };
}

I tried to look at the issues and PR but I really don't understand how the home.persistence is supposed to work in this situation. I'd appreciate hints or help to get it to work.

rapenne-s commented 2 years ago

I solved my issue, by re-reading the documentation and understanding my flake.nix file, it became clear the home.persistence.path had to be under the home-manager configuration, I was also lacking how to import home-manager.nix from the impermanence flake, I found the syntax.

Here is my working flake.nix file using impermanence and home-manager extension.

{
  inputs = {
    nixpkgs.url = "nixpkgs/nixos-unstable";

    home-manager = {
      url = "github:nix-community/home-manager";
      inputs.nixpkgs.follows = "nixpkgs";
    };

    impermanence = {
      url = "github:nix-community/impermanence";
      inputs.nixpkgs.follows = "nixpkgs";
    };
  };

  outputs = { self, nixpkgs, impermanence, home-manager, ... }@inputs:
    let
      system = "x86_64-linux";

      pkgs = import nixpkgs { inherit system; config = { allowUnfree = true; }; };
      lib = nixpkgs.lib;

    in
    {

      # begin nixosConfigurations
      nixosConfigurations = {
        t470 = lib.nixosSystem {
          inherit system;
          inherit lib;
          specialArgs = { inherit inputs; };

          # begin modules
          modules = [
            (import ./configuration.nix)

            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.solene = {
                home.username = "solene";
                home.homeDirectory = "/home/solene";
                imports = [ (inputs.impermanence + "/home-manager.nix") ];

                home.persistence."/nix/home/solene" = {
                  allowOther = true;
                  removePrefixDirectory = false;
                  directories = [
                    ".gnupg"
                    ".ssh"
                  ];
                  files = [
                    ".config/mpv/mpv.conf"
                    ".gitconfig"
                    ".tmux.conf"
                  ];
                }; # end home.persistence

              };
            }

            impermanence.nixosModules.impermanence
            {
              environment.persistence."/nix/" = {
                directories = [
                  "/etc/NetworkManager/system-connections/"
                  "/etc/nixos"
                  "/home/aria"
                  "/home/jeux"
                  "/home/baptiste"
                  "/nix/var/nix"
                  "/root"
                  "/var/lib"
                  "/var/log"
                  "/tmp"
                ];
              };
              environment.etc."machine-id".source = "/nix/etc/machine-id";
            }
          ];
        };
      };
    };
}