niv icon indicating copy to clipboard operation
niv copied to clipboard

Allow to configure niv directory path

Open uvNikita opened this issue 5 years ago • 12 comments

New NIV_DIR environment variable configures a path to a directory where niv will manage sources.{nix,json} files.

uvNikita avatar Oct 23 '19 18:10 uvNikita

@nmattia Is there anything one can contribute to this to get it merged? :)

kampka avatar Dec 16 '19 12:12 kampka

Oh wow! Apologies, first time I see this PR, must have missed the notification! Would the -s parameter work for you? See https://github.com/nmattia/niv/pull/159 for context

nmattia avatar Dec 16 '19 13:12 nmattia

Personally, I'd prefer the environment variable because I can ship that within a projects .envrc or similar, but that problem is easily solvable via a wrapper, so I'm not very attached to that specific solution.

kampka avatar Dec 16 '19 13:12 kampka

The main difference I guess would be that -s currently only places the sources.json into the source directory, the sources.nix is still placed into the nix folder. This PR will move sources.nix and sources.json to wherever NIV_PATH points to, which is what I would want out of this.

kampka avatar Dec 16 '19 13:12 kampka

In #172 I asked for this same feature as a CLI option instead of an environment variable. I think I'd like to do both because the user gets better documentation with help and doesn't need to worry about configuration via another mechanism. But for people doing non-interactive things with .envrc they can have their convenience too.

I may make a PR that implements both the env var and extra command-line switch.

shajra avatar Jan 18 '20 16:01 shajra

~~@nmattia The use-case this PR solves for me is when I want to use niv to manage multiple sources in one project independently. E.g. I use https://github.com/krebs/krops for deployment of multiple NixOS machines and I would like to have a separate sources.nix file per host.~~

~~I guess a workaround would be to use a single sources.nix file, but change the name of dependencies, e.g. nixpkgs-host1, nixpkgs-host2 etc.~~

I just realized that sources.nix can be configured with sourcesFile parameter:

import sources.nix { sourcesFile = PATH ; };

uvNikita avatar Aug 12 '20 20:08 uvNikita

I'd really to have NIV_DIR as I use direnv and like to layout my projects with a kind of XDG directory layout, keeping my Nix files in .config/nix. e.g.

$ tree .config/
.config/
└── nix
    ├── shell.nix
    ├── sources.json
    └── sources.nix

1 directory, 3 files

When I use --sources-files, I get the following warning

$ niv --sources-file .config/nix/sources.json update nixpkgs
Update nixpkgs
  WARNING: Could not read nix/sources.nix
     ( nix/sources.nix: openBinaryFile: does not exist (No such file or directory) )
Done: Update nixpkgs

I'm not sure what the downside of the warning is.

I currently workaround with

$ (cd .config && niv update nixpkgs)
Update nixpkgs
Done: Update nixpkgs

steshaw avatar Oct 04 '20 22:10 steshaw

@steshaw that's interesting, can you tell me more about the use case? Do you use niv globally, as opposed to per-project?

nmattia avatar Oct 05 '20 08:10 nmattia

Projects

With projects, I provision the [development] environment using direnv+nix+nix-direnv (though I used Lorri also for caching). In an example project .envrc is like this:

export PROJECT_HOME=$PWD
nix_shell=$PROJECT_HOME/.config/nix/shell.nix
use nix "${nix_shell}"
watch_file "${nix_shell}"
PATH_add scripts

The shell.nix is like this https://gist.github.com/steshaw/5ee72fd409e9cdc87deafb3e59096410. Aside: I'm not sure if I need the niv overlay when importing nixpkgs. IIRC, it'd compile the latest niv when I installed my user packages and I prefer to rely on fetching prebuilt binaries from the cache because I occasionally use my user packages on an underpowered MacBook Air.

Being able to add

export NIV_DIR=$PWD/.config/nix

to my .envrc has the benefit that I could be in any directory of my project and still issue niv commands and have niv find the sources.json along with the sources.nix.

I wrote up an unpublished article about how I use XDG-style layout for projects. I still use this layout as much as possible today, though I've moved PROJECT_CACHE_HOME to PROJECT_HOME/.cache, so it matches more closely with XDG and seems easier to remove with rm -rf .cache without accidentally deleting your .local directory :smile:.

Global

Not entirely sure what you mean by global but I have 2 candidates: global NixOS configuration and my user profile. I don't use niv for NixOS configuration (yet). I've just started to use it to pin nixpkgs for my user packages. My user packages configuration is in a project/repo that I share across my desktop (NixOS) and laptops macOS. I don't use a project-based XDG layout for this. I'm not using home-manager or nix-darwin but instead, have adapted lnl-overlay. These configurations can be found the nix subdirectory of my "dotfiles" repo https://github.com/steshaw/shelly/tree/master/nix.

Are you thinking that NIV_DIR could be used globally too? That would make sense. I'd set it up in my .profile to point to my the configuration for my user profile:

export NIV_DIR=~/Code/steshaw/shelly/nix/nix

If I also used niv for my NixOS configuration, then I'd have to choose. Since my user packages change more frequently than my NixOS, I'd probably leave it at that.

steshaw avatar Oct 05 '20 21:10 steshaw

(though I used Lorri also for caching)

Then you may want to give https://github.com/nmattia/sorri a try! It's a work in progress but we already use it at $WORK.

I use XDG-style layout for projects.

Ok, that makes a lot of sense. I want to spend some time cleaning up https://github.com/nmattia/niv/pull/279 this week, I might as well add support for NIV_DIR.

nmattia avatar Oct 06 '20 08:10 nmattia

Thanks! I'll be sure to check out https://github.com/nmattia/sorri as well 😄

steshaw avatar Oct 06 '20 08:10 steshaw

Thanks for sharing your work-around @steshaw! Initially I'd hoped to set NIV_DIR to .nix, but I've moved to using .config/nix.

Addng DirEnv to the mix, this is loaded by adding use nix .config/nix/shell.nix to the project.envrc.

Looking forward to when this PR is merge so that Niv mgmt will be more "normalized".

Thanks!

nickgarber avatar Dec 20 '21 15:12 nickgarber