nixd icon indicating copy to clipboard operation
nixd copied to clipboard

Alternative Flake configuration for completions

Open thiagokokada opened this issue 1 year ago • 0 comments

I was trying to find a way to make NixOS/Home-Manager/nix-darwin completion in a Flake-based configuration more "generic", in a way that it would work in any hostname that I used without depending of my configuration being in a specific PATH. This is what I ended up having (for neovim-lsp, but can easily be adapted for something else):

{
  formatting = {
    command = { "nixfmt" },
  },
  options = {
    nixos = {
      expr = [[
        (let
          pkgs = import "${flake.inputs.nixpkgs}" { };
          inherit (pkgs) lib;
        in (lib.evalModules {
          modules = (import "${flake.inputs.nixpkgs}/nixos/modules/module-list.nix");
          check = false;
        })).options
      ]],
    },
    nix_darwin = {
      expr = [[
        (let
          pkgs = import "${flake.inputs.nixpkgs}" { };
          inherit (pkgs) lib;
        in (lib.evalModules {
          modules = (import "${flake.inputs.nix-darwin}/modules/module-list.nix");
          check = false;
        })).options
      ]],
    },
    home_manager = {
      expr = [[
        (let
          pkgs = import "${flake.inputs.nixpkgs}" { };
          lib = import "${flake.inputs.home-manager}/modules/lib/stdlib-extended.nix" pkgs.lib;
        in (lib.evalModules {
          modules =  (import "${flake.inputs.home-manager}/modules/modules.nix") {
            inherit lib pkgs;
            check = false;
          };
        })).options
      ]],
    },
  },
  diagnostic = {
    suppress = {
      "sema-escaping-with"
    },
  },
}

So the idea is that instead of importing a specific configuration and evaluating it, we basically use evalModules to evaluate all modules available in modules-list.nix or modules.nix.

It seems to work well enough in my case, but it has the obvious downside of using a implementation detail that can change in future (evalModules even has a comment saying that check should be removed, but I don't think this will ever happen). Anyway, most sharing, maybe this could be added in the documentation unless there is some downsides that I am not aware of.

thiagokokada avatar Feb 14 '25 00:02 thiagokokada