Nix flakes and submodules

Hello,

I am playing with flakes new feture. I am testing on one project. This project has git submodules with “my tipical cmake scripts”. My nix flake is:

{                                                                                                                                                                                        
  description = "My lib";                                                                                                                                        
                                                                                                                                                                                         
  outputs = { self, nixpkgs }: {                                                                                                                                                         
                                                                                                                                                                                         
    defaultPackage.x86_64-linux =                                                                                                                                                        
    with import nixpkgs { system = "x86_64-linux"; };                                                                                                                                    
    stdenv.mkDerivation {                                                                                                                                                                
      name = "my_lib";                                                                                                                                                                     
      version = "0.1.0";                                                                                                                                                                 
      src = self;                                                                                                                                                                        
                                                                                                                                                                                         
      outputs = [ "out" "dev" ];                                                                                                                                                         
                                                                                                                                                                                         
      nativeBuildInputs = [ cmake ];                                                                                                                                                  
      buildInputs = [                                                                                                                                                                    
        spdlog                                                                                                                                                                           
        fmt                                                                                                                                                                              
        nlohmann_json                                                                                                                                                                    
        doctest                                                                                                                                                                          
      ];                                                                                                                                                                                 
    };                                                                                                                                                                                   
  };                                                                                                                                                                                     
}  

I get this error:

nix build -v                                                                                                                                                                 !10342
warning: Git tree '/home/eduardo/dev/clit' is dirty
building '/nix/store/y8pbpdnx50cy14i05irbbmy13wkxnv06-clit.drv'...
builder for '/nix/store/y8pbpdnx50cy14i05irbbmy13wkxnv06-clit.drv' failed with exit code 1; last 10 log lines:
  -- Detecting CXX compile features
  -- Detecting CXX compile features - done
  CMake Error at CMakeLists.txt:16 (include):
    include could not find load file:
  
      GenericProject
  
  
  -- Configuring incomplete, errors occurred!
  See also "/build/source/build/CMakeFiles/CMakeOutput.log".
error: build of '/nix/store/y8pbpdnx50cy14i05irbbmy13wkxnv06-clit.drv' failed

The problem is that build cannot find the file GenericProject.cmake that is in the submodule. How can i get this working?

While nix gained the feature to include submodules on a builtins.fetchGit in Add fetchSubmodules to builtins.fetchGit by blitz · Pull Request #3166 · NixOS/nix · GitHub, we’ll need to wait for the next release to actually make use of it. (Or you install unstable nix)

Edit: Oops. I just realized you are using nix from the flakes branch. Hm. Might be a different issue then.

I see on the pull request that by default submodules is disabled. There is any way to configure something like this:

 src= {url= self; submodules=true;};
1 Like

Since this is one of the top search results, reposting here as well:

Nowadays (2.6.0), you can use nix build '.?submodules=1'

4 Likes

Thanks for the update @cab, it helped me! I didn’t understand how to specify a different target, but it goes like this: nix build '.?submodules=1#myPackage'. (Weirdly swapping the order of target and option doesn’t work).

But I still wonder how I can set this option “globally” for my flake, so that I don’t have to specify this option all the time. It looks like what @chanilino mentioned up there should do it, but I wouldn’t know where to put it.

You can use registries to alias whichever flake uri you want to a string.
For instance, here’s registry entry for blender-bin flake: https://github.com/NixOS/flake-registry/blob/8acd341882ba495f04b14d7f11073b8a97197e8d/flake-registry.json#L14

Notice "dir": "blender". You can also add "submodules": "1" there.
man nix3-registry for more info

1 Like