Install custom derivation using flakes

I made a derivation for a program, which is not in nixpkgs. It has a myapp/default.nix, and I usually just use callPackage and can then install the program in home-manager.

#overlays.nix
[
  (
    self: super:
      let
        callPackage = self.callPackage;
      in
      {
         myapp = callPackage ./myapp { };
        };
      }
  )
]

Now I am switching to flakes. My flake.nix looks basically as in the home-manager example.

{
  description = "NixOS configuration";

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

  outputs = { home-manager, nixpkgs, ... }: {
    nixosConfigurations = {
      hostname = nixpkgs.lib.nixosSystem {
        system = "x86_64-linux";
        modules = [
          ./configuration.nix
          home-manager.nixosModules.home-manager
          {
            home-manager.useGlobalPkgs = true;
            home-manager.useUserPackages = true;
            home-manager.users.jdoe = import ./home.nix;
          }
        ];
      };
    };
  };
}

How can I install my custom derivation via home-manager using flakes? Does anyone have a MWE?

I don’t use home manager, but maybe this will help you.
nixpkgs.lib.nixosSystem takes a pkgs argument. You can use the nixpkgs input to construct a pkgs that has your custom package in it with an overlay. Here is an example.
defining pkgs
calling nixosSystem

Move that into your flake as a package output, then make sure that you add _module.args.self = self; to one of your modules, and also pass self to your ./home.nix import, there you should also set _module.args.selg = self;.

You will get self as an argument to all modules then and make it accessible by adding it to the argset.

That can be used then to add self.packages.${system].hello to your home.packages.

I got it working. Here is my flake.nix:

{
  description = "NixOS configuration";

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

  outputs = inputs @ { nixpkgs, home-manager, ... }:
    let
      system = "x86_64-linux";
      pkgs = import nixpkgs {
        system = system;
        config = { allowUnfree = true; };
        overlays = [
          (final: prev: {
            myapp = final.callPackage ./pkgs/myapp { };
            vscode-extensions = final.lib.recursiveUpdate prev.vscode-extensions {
              monokai.theme-monokai-pro-vscode = final.vscode-utils.extensionFromVscodeMarketplace {
                name = "theme-monokai-pro-vscode";
                publisher = "monokai";
                version = "1.1.19";
                sha256 = "sha256-haCxXYRAhUI3Kc6YvaarXKBd7/KcCWsd53wuZF7zf2o=";
              };
            };
          })
        ];
      };
    in
    {
      nixosConfigurations = {
        me = nixpkgs.lib.nixosSystem {
          inherit system pkgs;
          modules = [
            ./machines/me/configuration.nix
            home-manager.nixosModules.home-manager
            {
              home-manager.useGlobalPkgs = true;
              home-manager.useUserPackages = true;
              home-manager.users.me = import ./users/machines/me.nix;
            }
          ];
        };
      };
    };
}
1 Like