Can I use flakes within a git repo without committing flake.nix?

ok, now I know that

  1. nix develop . on a folder with .git is identical to nix develop git+file:. (NOTE: not to nix develop git:. and not to nix develop file:.)

  2. and nix develop . on a folder without .git is identical to nix develop path:.

Example

I have an empty dir with two files (from Super fast nix-shell in Flakes - NixOS Wiki), git add was not yet called

shell.nix

{ pkgs ? import <nixpkgs> { } }:
with pkgs;
mkShell {
  buildInputs = [
    nixpkgs-fmt
  ];

  shellHook = ''
    # ...
  '';
}

flake.nix

{
  description = "my project description";

  inputs.flake-utils.url = "github:numtide/flake-utils";

  outputs = { self, nixpkgs, flake-utils }:
    flake-utils.lib.eachDefaultSystem
      (system:
        let pkgs = nixpkgs.legacyPackages.${system}; in
        {
          devShells.default = import ./shell.nix { inherit pkgs; };
        }
      );
}
$ git init
Initialized empty Git repository in /home/srghma/projects/hello/.git/

$ git status
On branch main

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        flake.nix
        shell.nix

nothing added to commit but untracked files present (use "git add" to track)

$ nix develop .
warning: Git tree '/home/srghma/projects/hello' is dirty
error: getting status of '/nix/store/0ccnxa25whszw7mgbgyzdm4nqc0zwnm8-source/flake.nix': No such file or directory

$ nix develop git+file:.
warning: Git tree '.' is dirty
error: getting status of '/nix/store/0ccnxa25whszw7mgbgyzdm4nqc0zwnm8-source/flake.nix': No such file or directory

$ nix develop path:.
warning: creating lock file './flake.lock'

[srghma@machine:~/projects/hello]$

$ cat flake.lock
{
  "nodes": {
    "flake-utils": {
      "inputs": {
        "systems": "systems"
      },
      "locked": {
        "lastModified": 1681202837,
        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1685399834,
        "narHash": "sha256-Lt7//5snriXSdJo5hlVcDkpERL1piiih0UXIz1RUcC4=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "58c85835512b0db938600b6fe13cc3e3dc4b364e",
        "type": "github"
      },
      "original": {
        "id": "nixpkgs",
        "type": "indirect"
      }
    },
    "root": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nixpkgs": "nixpkgs"
      }
    },
    "systems": {
      "locked": {
        "lastModified": 1681028828,
        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
        "owner": "nix-systems",
        "repo": "default",
        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
        "type": "github"
      },
      "original": {
        "owner": "nix-systems",
        "repo": "default",
        "type": "github"
      }
    }
  },
  "root": "root",
  "version": 7
}
$ rm -frd flake.lock
$ nix develop path:.
warning: creating lock file './flake.lock'

[srghma@machine:~/projects/hello]$
exit
$ cat flake.lock
{
  "nodes": {
    "flake-utils": {
      "inputs": {
        "systems": "systems"
      },
      "locked": {
        "lastModified": 1681202837,
        "narHash": "sha256-H+Rh19JDwRtpVPAWp64F+rlEtxUWBAQW28eAi3SRSzg=",
        "owner": "numtide",
        "repo": "flake-utils",
        "rev": "cfacdce06f30d2b68473a46042957675eebb3401",
        "type": "github"
      },
      "original": {
        "owner": "numtide",
        "repo": "flake-utils",
        "type": "github"
      }
    },
    "nixpkgs": {
      "locked": {
        "lastModified": 1685399834,
        "narHash": "sha256-Lt7//5snriXSdJo5hlVcDkpERL1piiih0UXIz1RUcC4=",
        "owner": "NixOS",
        "repo": "nixpkgs",
        "rev": "58c85835512b0db938600b6fe13cc3e3dc4b364e",
        "type": "github"
      },
      "original": {
        "id": "nixpkgs",
        "type": "indirect"
      }
    },
    "root": {
      "inputs": {
        "flake-utils": "flake-utils",
        "nixpkgs": "nixpkgs"
      }
    },
    "systems": {
      "locked": {
        "lastModified": 1681028828,
        "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
        "owner": "nix-systems",
        "repo": "default",
        "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
        "type": "github"
      },
      "original": {
        "owner": "nix-systems",
        "repo": "default",
        "type": "github"
      }
    }
  },
  "root": "root",
  "version": 7
}

$ rm -frd flake.lock
$ git add --all
$ git status
On branch main

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   flake.nix
        new file:   shell.nix

$ nix develop git:.
ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
warning: could not read HEAD ref from repo at 'git:.', using 'master'
evaluating derivation 'git:.#devShells.x86_64-linux.default'ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: program 'git' failed with exit code 128
(use '--show-trace' to show detailed location information)
$ nix develop file:.
error: unable to download 'file:.': URL using bad/illegal format or missing URL (3)
(use '--show-trace' to show detailed location information)

$ nix develop git+file:.
warning: Git tree '.' is dirty
warning: creating lock file './flake.lock'
warning: Git tree '.' is dirty

[srghma@machine:~/projects/hello]$
exit

Example 2 (use `nix develop git+file:.` on a folder without `.git`)
$ rm -frd flake.lock
$ rm -frd .git
$ nix develop .
warning: creating lock file '/home/srghma/projects/hello/flake.lock'

[srghma@machine:~/projects/hello]$
exit
$ ls
flake.lock  flake.nix  shell.nix
$ rm -frd flake.lock
$ nix develop git+file:.
ssh: Could not resolve hostname file: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
warning: could not read HEAD ref from repo at 'file:.', using 'master'
evaluating derivation 'git+file:.#devShells.x86_64-linux.default'ssh: Could not resolve hostname file: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: program 'git' failed with exit code 128
(use '--show-trace' to show detailed location information)
$ nix develop git:.
ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
warning: could not read HEAD ref from repo at 'git:.', using 'master'
evaluating derivation 'git:.#devShells.x86_64-linux.default'ssh: Could not resolve hostname git: Name or service not known
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.
error: program 'git' failed with exit code 128
(use '--show-trace' to show detailed location information)

$ nix develop file:.
error: unable to download 'file:.': URL using bad/illegal format or missing URL (3)
(use '--show-trace' to show detailed location information)

Question:

  1. how git is used for “type inference”?
  2. Why not to make the nix develop . on a folder with .git identical to nix develop path:. instead?

I still dont understand what throwing No such file or directory error in

$ nix develop .
warning: Git tree '/home/srghma/projects/hello' is dirty
error: getting status of '/nix/store/0ccnxa25whszw7mgbgyzdm4nqc0zwnm8-source/flake.nix': No such file or directory

gives us

4 Likes