Nixos-rebuild build-image fails with "default.nix': No such file or directory" when using flake flag

Trying to create my own lxc containers, and I am kind of running blind because the manual section for nixos-rebuild build-image doesn’t include any examples of usage.

Correct me if I’m wrong, but if I’m running the following command: nixos-rebuild build-image --flake . --image-variant lxc a default.nix shouldn’t be necessary no?

In case its pertinent, heres the flake:

{
  description = "Forgejo server";
  inputs = {
    nixpkgs.url = "github:nixos/nixpkgs/nixos-26.05";
  };
  outputs = { self, nixpkgs, ... }:
  let
    inputs = self.inputs;
    system = "x86_64-linux";
    forgejo-metadata = import ./forgejo-metadata.nix;
  in {
    nixosConfigurations.${forgejo-metadata.hostname} = nixpkgs.lib.nixosSystem rec {
      specialArgs = { inherit inputs; inherit nixpkgs; rootPath = self.outPath; };
      modules = [ ./configuration.nix ];
    };
  };
}

Looking for a sanity check before I post a bug report on github

We need a full example and the full error.

Dumb question, but have you tried --flake .#<value of forgejo-metadata.hostname>? And does configuration.nix have any references to ./default.nix?

Yes, I am including the hashtag and the hostname, and no it does not have any mention of a default.nix

I don’t really know what you mean by full example, but heres the trace of the error:

Sorry for the link, it seems there too many characters to post it directly here

Reading that full trace, I’m even more confused now, why does using import on a file thats just a set containing some values require a default.nix? The entire postgres-metadata.nix file is just { hostname = “aquaPostgres”; port = 5432; }

Now I see. Nix is parsing that as (import rootPath) + /../postgres/postgres-metadata.nix. If you’re using import a + b, you always want import (a + b). Importing a directory tries to import <dir>/default.nix.

1 Like

Thats almost certainly my issue, thank you. I really need to look into nix lsps and whether they could catch this this kind of thing with type inference

Not really, because import [path] can return literally any type.
You just kinda have to know operator precedence:

Path concat has precedence 7, and function application has precedence 2 (higher).

So f path1 + path2 gets treated as (f path1) + path2.

By full example I meant a repo with all of the files, not just the flake.nix, but in this case the error was enough to diagnose the issue (sometimes it’s not enough).

1 Like