Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add flakeDir attribute to flake self for flake.nix-relative paths. #4089

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

@kquick
Copy link
Contributor

@kquick kquick commented Sep 29, 2020

The self input passed to a flake is always set to the root of the
input tree (usually identified by the .git directory).

In repositories where the flake.nix is not at the top-level
directory (e.g. github:owner/project?dir=sub/path) the self
location relative to the flake.nix location is awkward to use and may
change over time. However, it is appropriate to fetch and cache the
source from the top-level directory of that source.

This patch adds a flakeDir attribute to self to allow the flake to
specify source locations relative to the flake.nix file instead of
relative to the top-level of the source.

This is particularly useful for repositories that host multiple
projects, where each sub-project has its own separate flake.nix file
for building that sub-project.

github:owner/foo flake.nix:
{
  description = "Flakes for foo utility";

  inputs.foo-cli.url = github:owner/foo?dir=subproj/foo_cli;
  inputs.foo-gnome-gui.url = github:owner/foo?dir=subproj/gnome_gui;

  outputs = { self, foo-cli, foo-gnome-gui }:
  rec {
    defaultPackage.x86_64-linux = packages.x86_64-linux.foo-gnome-gui;
    packages.x86_64-linux = {
      foo-gnome-gui = foo-gnome-gui;
      foo-cli = foo-cli;
    };
  };
}

github:owner/foo subproj/foo_cli:
{
  description = "Flake for foo cli utility";
  outputs = { self, nixpkgs }:
  rec {
    defaultPackage.x86_64-linux = packages.x86_64-linux.foo-cli;
    packages.x86_64-linux =
      let pkgs = import nixpkgs { system="x86_64-linux"; }; in
      {
        foo-cli = mkDerivation {
          name = "foo-cli";
          src = self.flakeDir;
        };
      };
  };
}
The `self` input passed to a flake is always set to the root of the
input tree (usually identified by the `.git` directory).

In repositories where the `flake.nix` is not at the top-level
directory (e.g. `github:owner/project?dir=sub/path`) the `self`
location relative to the `flake.nix` location is awkward to use and may
change over time.  However, it is appropriate to fetch and cache the
source from the top-level directory of that source.

This patch adds a `flakeDir` attribute to `self` to allow the flake to
specify source locations relative to the `flake.nix` file instead of
relative to the top-level of the source.

This is particularly useful for repositories that host multiple
projects, where each sub-project has its own separate `flake.nix` file
for building that sub-project.

```
github:owner/foo flake.nix:
{
  description = "Flakes for foo utility";

  inputs.foo-cli.url = github:owner/foo?dir=subproj/foo_cli;
  inputs.foo-gnome-gui.url = github:owner/foo?dir=subproj/gnome_gui;

  outputs = { self, foo-cli, foo-gnome-gui }:
  rec {
    defaultPackage.x86_64-linux = packages.x86_64-linux.foo-gnome-gui;
    packages.x86_64-linux = {
      foo-gnome-gui = foo-gnome-gui;
      foo-cli = foo-cli;
    };
  };
}

github:owner/foo subproj/foo_cli:
{
  description = "Flake for foo cli utility";
  outputs = { self, nixpkgs }:
  rec {
    defaultPackage.x86_64-linux = packages.x86_64-linux.foo-cli;
    packages.x86_64-linux =
      let pkgs = import nixpkgs { system="x86_64-linux"; }; in
      {
        foo-cli = mkDerivation {
          name = "foo-cli";
          src = self.flakeDir;
        };
      };
  };
}
```
@kquick
Copy link
Contributor Author

@kquick kquick commented Oct 28, 2020

@edolstra , this is ready to go and would be good to get merged.

@kquick
Copy link
Contributor Author

@kquick kquick commented Nov 26, 2020

This change is useful for situations where separate repositories are merged into a single repository with subdirectories. Because there may have been independent uses of the separate repositories (e.g. downstream that might use one but not need or be aware of the other), maintaining a flake file in the independent subdirectories maintains this independence.

This change provides a flakeDir attribute that can be used for src or other needs that is relative to the flake itself, not relative to the root of the repository.

@edolstra, does this seem like a reasonable enhancement?

@andir
Copy link
Member

@andir andir commented Nov 26, 2020

This feels weird. Nix already support path (./.). Why can't we just make those work in flakes instead of reinventing the wheel? We should try to invent less magic and/or (undocumented) attributes that have special meaning.

@zimbatm
Copy link
Member

@zimbatm zimbatm commented Nov 28, 2020

Adding support for paths would also allow builtins.getFlake ./. to do the expected thing in the REPL.

@stale
Copy link

@stale stale bot commented Jun 2, 2021

I marked this as stale due to inactivity. → More info

@stale stale bot added the stale label Jun 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

3 participants