Skip to content

Allow nix run and friends to fall back to default.nix #15701

@hsjobeki

Description

@hsjobeki

Is your feature request related to a problem?

Projects without flake.nix currently get a degraded UX in the cli

For example instead of just nix run .#hello users have to type nix run -f default.nix hello
But even with this command it is not equivalent behavior.

missing:

  • pure eval
  • cannot run from url specifiers such as github:owner/name

For better understanding i created a simple repo https://github.com/hsjobeki/not-a-flake/

This forces every project to adopt flakes to use the new CLI, even when a simple default.nix would suffice.

There is also this related PR #15661 (from @KiaraGrouwstra )

Proposed solution

nix commands should fall back to resolving to default.nix when the flake.nix resolution failed.

Proposed behavior:

  • If flake.nix exists: use it (unchanged)
  • If only default.nix exists: evaluate it as the entry point
  • If neither exists keep the error
  • Both path: URLs and bare paths (., /foo/bar) should work
  • store-copying:
      1. local path refs: No store copying and no git root detection. Pure eval like --pure-eval --include PWD
      1. remote sources such as github:owner/name copy to the store. Check if the source has flake.nix and use default.nix as fallback.
  • If default.nix can be a function accepting { system ? builtins.currentSystem, ... }, auto-pass system = currentSystem to bypass builtins.currentSystem`.
  • Fallback chain for attributes
    1. Use the fragment given (.#hello)
    1. Check if default.nix exposes a derivation at top-level: Yes -> run that.
    1. Use "default" attribute (leave out in first PR, decide in a later PR)

On the experimental flags:

  • flakes disabled: can still use nix run on default.nix
  • flakes enabled: flakes take full precedence $pwd/default.nix acts as fallback when flake.nix is not found during the git repo traversal logic.

Test suite in #15707 in case anything needs to be changed or specified in depth.

Alternative solutions

Open for alternative suggestions that don't degrade UX and let people use "nix run" on remote uris

Concerns

Risk of implicitly stabilizing flakes features that are not agreed on yet.

I would like to keep this design as minimal as possible. Leaving out the following, because they are controversial:

  • No git root detection. Root detection should be agnostic from git to allow usage in non-git vcs.
  • No subdirectory traversal towards the project root. Just run $pwd/default.nix. We can add this later.

Checklist


Add 👍 to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureFeature request or proposal
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions