# Get Nix Flake to include git submodule

**URL:** https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324
**Category:** Help
**Created:** [July 11, 2023, 9:34pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324 "2023-07-11T21:34:11Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![WinstonH](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/w/9fc348/32.png) [@WinstonH](https://discourse.nixos.org/u/WinstonH)
#### Post date: [July 11, 2023, 9:34pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/1 "2023-07-11T21:34:11Z")

</div>

I have a directory that looks like this:

```auto
.git/
submodule/
| .git/
| ...
stuff/
flake.nix
...

```

I want the submodule (which is not a flake) to be included in the flake source directory. What’s the best way to do this? I’ve tried the various permutations of url options from [“nix flakes: add support for git submodules”](https://github.com/NixOS/nix/issues/4423#).

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [July 12, 2023, 10:36am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/2 "2023-07-12T10:36:39Z")

</div>

Use `submodules=1` in the query.

If this does not work, please share a minimal reproducible example that we can do some testing with.

---

<div class="post-metadata">

### Author: ![WinstonH](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/w/9fc348/32.png) [@WinstonH](https://discourse.nixos.org/u/WinstonH)
#### Post date: [July 13, 2023, 10:51pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/3 "2023-07-13T22:51:41Z")

</div>

What’s the input `url` supposed to be for a submodule in the flake’s directory? Checking out from remote works (very inefficient), and this specific combination of:

```auto
url = "file:///[snip]/submodule?submodules=1";
type = "git";

```

works, but these failed:

```auto
inputs.submodule = {
    flake = false;
    url = "git+file:///[snip]/submodule?submodules=1";
}; # Fails w/ `error: opening file '[snip]/submodule/.git/config': Not a directory`

inputs.submodule = {
    flake = false;
    url = "git+path:submodule?submodules=1";
}; # Unsupported

inputs.submodule = {
    flake = false;
    url = "path:submodule?submodules=1";
    type = "git";
}; # Unsupported

```

I’d rather not have to specify the absolute path to the submodule.

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [July 14, 2023, 5:02am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/4 "2023-07-14T05:02:37Z")

</div>

You do not add the submodule itself as an input, you need to add the `submodules=1` query parameter to the input that has the submodule.

If this is your “`self`”, you need to provide the query parameter on each CLI invocation.

```plaintext
nix build '.?submodules=1'

```

---

<div class="post-metadata">

### Author: ![WinstonH](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/w/9fc348/32.png) [@WinstonH](https://discourse.nixos.org/u/WinstonH)
#### Post date: [July 14, 2023, 5:37am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/5 "2023-07-14T05:37:37Z")

</div>

That worked! And is… very unintuitive.

EDIT: Is there any way to avoid manually specifying that every time?

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [July 14, 2023, 6:23am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/6 "2023-07-14T06:23:30Z")

</div>

The only way I am aware of that would remove the necessity of specifying `submodules=1` is to remove the submodule.

And I do not consider this unintuitive. You have to specify the argument to the input that has the submodules, if this input is `self`, then the only place where you can specify that is the CLI.

---

<div class="post-metadata">

### Author: ![WinstonH](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/w/9fc348/32.png) [@WinstonH](https://discourse.nixos.org/u/WinstonH)
#### Post date: [July 14, 2023, 7:07am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/7 "2023-07-14T07:07:03Z")

</div>

I’m definitely a nix novice, so you probably know more than me. But, I don’t think I’ve ever passed `?something` to the nix CLI before and it’s undocumented AFAIK (excluding the github issue where people are similarly confused).

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [July 14, 2023, 7:18am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/8 "2023-07-14T07:18:19Z")

</div>

I agree that it is not convinient. Still, it is not unintuitive, as this is the only place where you specify `self`s URL.

The CLI is also where you are able to change its schema.

Though my stance remains: the best way to deal with git submodules is to get rid of them…

---

<div class="post-metadata">

### Author: ![chreekat](https://discourse.nixos.org/user_avatar/discourse.nixos.org/chreekat/32/14_2.png) [@chreekat](https://discourse.nixos.org/u/chreekat)
#### Post date: [October 8, 2023, 2:34pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/9 "2023-10-08T14:34:06Z")

</div>

> [@NobbZ](#):
>
> Still, it is not unintuitive, as this is the only place where you specify `self`s URL.

I guess “intuitive” is in the eye of the beholder, because the only place I specify `self`’s url is… nowhere. I don’t specify it. I expect `nix develop` (and direnv’s `use flake`) to just work. 🙂

---

<div class="post-metadata">

### Author: ![NobbZ](https://discourse.nixos.org/user_avatar/discourse.nixos.org/nobbz/32/7022_2.png) [@NobbZ](https://discourse.nixos.org/u/NobbZ)
#### Post date: [October 8, 2023, 2:35pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/10 "2023-10-08T14:35:56Z")

</div>

There you implicitely specify `.`. If you want to use submodules, you have to be explicit about that.

---

<div class="post-metadata">

### Author: ![chreekat](https://discourse.nixos.org/user_avatar/discourse.nixos.org/chreekat/32/14_2.png) [@chreekat](https://discourse.nixos.org/u/chreekat)
#### Post date: [October 8, 2023, 2:38pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/11 "2023-10-08T14:38:30Z")

</div>

Ok, I understand, but I do consider this to mean that flakes just don’t properly support git submodules and will work around it to the best of my ability.

---

<div class="post-metadata">

### Author: ![siriobalmelli](https://discourse.nixos.org/user_avatar/discourse.nixos.org/siriobalmelli/32/4696_2.png) [@siriobalmelli](https://discourse.nixos.org/u/siriobalmelli)
#### Post date: [February 15, 2024, 8:37am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/12 "2024-02-15T08:37:20Z")

</div>

I’m running into this also.

In a real-world context, requiring users of my flake to know that they must add a “special” command-line parameter `?submodules=1` creates a condition where things “don’t work” by default.

I imagine it should be possible to set `submodules=true` _within_ the flake, but my attempt so far yields an error:

```nix
  outputs = {
    self
  } @ inputs: let
    # error: attribute 'override' missing
    self = inputs.self.override (old: { submodules = true; }); # overrideAttrs also doesn't work
  in
    {
      # outputs
    }

```

Another approach might be to specify the local directory as an input, and give it a `submodules=1` parameter there.

This input specification works, but without including the submodule:

```nix
  inputs = {
    src = {
      url = "file:.?submodules=1";
      type = "git";
      flake = false; # not including this results in inifite recursion
    };
  };

```

Another approach (and this may be the cleanest?) would be to set `submodules` as a toplevel flake variable, eg:

```nix
{
  submodules = true;

  inputs = {};
}

```

Interestingly, a `nix repl` _already_ sees the submoduels toplevel attribute:

```auto
╰─➤ nix repl 1 ↵
Welcome to Nix 2.18.1. Type :? for help.

nix-repl> :lf .
Added 14 variables.

nix-repl> submodules
false

nix-repl> inputs
{ flake-utils = { ... }; nixpkgs = { ... }; }

nix-repl>

```

---

<div class="post-metadata">

### Author: ![siriobalmelli](https://discourse.nixos.org/user_avatar/discourse.nixos.org/siriobalmelli/32/4696_2.png) [@siriobalmelli](https://discourse.nixos.org/u/siriobalmelli)
#### Post date: [February 22, 2024, 12:32pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/13 "2024-02-22T12:32:27Z")

</div>

Looks like I’m not the only one seeing this.

The upshot is that as of Nix 2.20 the need for `?submodules=1` will go away entirely: [feat: support managing submodules via inputs.self by tomberek · Pull Request #7862 · NixOS/nix · GitHub](https://github.com/NixOS/nix/pull/7862#issuecomment-1908577578)

---

<div class="post-metadata">

### Author: ![jakubgs](https://discourse.nixos.org/user_avatar/discourse.nixos.org/jakubgs/32/2009_2.png) [@jakubgs](https://discourse.nixos.org/u/jakubgs)
#### Post date: [March 30, 2024, 10:46am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/14 "2024-03-30T10:46:14Z")

</div>

I think the fact that there’s no way to make `self` include submodules without having to inform the developer/user to specify `?submodules=1` is both unintuitive and inconvenient.

A normal behavior would be to call:

```sh
nix build github:example-org/example-pkg

```

And receive a built `example-pkg` binary. But instead you get errors about missing submodules.

Yes, it is unintuitive and this should be configurable in the `flake.nix` file.

I see someone has made an attempt to support this, but it’s not really moving:

- [feat: support managing submodules via inputs.self by tomberek · Pull Request #7862 · NixOS/nix · GitHub](https://github.com/NixOS/nix/pull/7862)

---

<div class="post-metadata">

### Author: ![Krusl\_Lee](https://discourse.nixos.org/letter_avatar_proxy/v4/letter/k/c0e974/32.png) [@Krusl\_Lee](https://discourse.nixos.org/u/Krusl_Lee)
#### Post date: [April 12, 2024, 12:10pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/15 "2024-04-12T12:10:04Z")

</div>

I’m wondering how can I use a flake as an input for my system configuration, where such a flake needs “?submodules=1” to build?

---

<div class="post-metadata">

### Author: ![siriobalmelli](https://discourse.nixos.org/user_avatar/discourse.nixos.org/siriobalmelli/32/4696_2.png) [@siriobalmelli](https://discourse.nixos.org/u/siriobalmelli)
#### Post date: [April 23, 2024, 9:05am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/16 "2024-04-23T09:05:06Z")

</div>

This worked for me, but haven’t tested with a remote eg. github:

```auto
url = "git+file:///PATH?submodules=1";

```

---

<div class="post-metadata">

### Author: ![siriobalmelli](https://discourse.nixos.org/user_avatar/discourse.nixos.org/siriobalmelli/32/4696_2.png) [@siriobalmelli](https://discourse.nixos.org/u/siriobalmelli)
#### Post date: [April 24, 2024, 5:36am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/17 "2024-04-24T05:36:55Z")

</div>

I’ve realized that one way to avoid the `.?submodules=1#` UX nightmare is to declare the submodule as a discrete file-based input:

```auto
inputs = {
  # ...
  subproject = {
    url = "git+file:subproject"; # the submodule is in the ./subproject dir
    flake = false;
  };
}

```

Then, any derivation(s) which rely on the submodule need to be extended to take the submodule as an input and need to deal with it manually; eg:

```auto
  buildPhase = let
    prefix = python3.sitePackages; # eg: lib/python3.11/site-packages
  in ''
    # copy entire package as a library
    mkdir -p $out/${prefix}
    ${rsync}/bin/rsync -rl ${src}/ $out/${prefix}/
    # same for 'subproject' submodule
    mkdir -p $out/${prefix}/subproject
    ${rsync}/bin/rsync -rl ${subproject}/ $out/${prefix}/subproject/

    # rest of build phase

```

This will work locally and in CI, as long as the submodule is checked out.

Then, when passing this toplevel project flake as an input to another flake, use `submodules=1`, eg:

```auto
      url = "github:owner/project_name?ref=branch/name/with/slashes&submodules=1";

      # this alternative also works:
      url = "git+ssh://git@github.com/owner/project_name.git?ref=branch/name/with/slashes&submodules=1";

```

---

<div class="post-metadata">

### Author: ![paull](https://discourse.nixos.org/user_avatar/discourse.nixos.org/paull/32/26813_2.png) [@paull](https://discourse.nixos.org/u/paull)
#### Post date: [June 23, 2024, 2:22pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/18 "2024-06-23T14:22:25Z")

</div>

Hey, dont wanna hijack your thread but do I need to build the subproject after referencing as input?  
Using the suggestions from your last answer:

```auto
inputs = {
  # ...
  subproject = {
    url = "git+file:subproject"; # the submodule is in the ./subproject dir
    flake = false;
  };
}

```

but all I get is a error message with “/subproject: No such file or directory”. Already update, synced and reinit the submodule.

---

<div class="post-metadata">

### Author: ![siriobalmelli](https://discourse.nixos.org/user_avatar/discourse.nixos.org/siriobalmelli/32/4696_2.png) [@siriobalmelli](https://discourse.nixos.org/u/siriobalmelli)
#### Post date: [June 25, 2024, 10:10am UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/19 "2024-06-25T10:10:10Z")

</div>

hmm, this really should work as long as the submodule:

- is present in `.gitmodules` (`git submodule add URL`)
- the commit adding the submodule to `.gitmodules` and pinning its version is present in the superproject’s git history (`git add ./submodule && git commit`)
- the submodule is checked out (files present on disk: `git submodule update --init`)

Let me know if that solves it

---

<div class="post-metadata">

### Author: ![paull](https://discourse.nixos.org/user_avatar/discourse.nixos.org/paull/32/26813_2.png) [@paull](https://discourse.nixos.org/u/paull)
#### Post date: [July 3, 2024, 10:05pm UTC](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324/20 "2024-07-03T22:05:52Z")

</div>

Thanks for your response. Tried everything you have listed and a lot more but did not got it working unfortunately. Fixed it with a workaround fetching the git repo now from remote instead using a submodules, works flawlessly for my purpose.

[Next page](https://discourse.nixos.org/t/get-nix-flake-to-include-git-submodule/30324.md?page=2)
