Where does nix-env query for available packages? - nix

I know you can query for available packages with:
nix-env -qa search_term
from man
I'm curios, how is nix-env doing the search? Is it searching in my /nix/store or will perform some request on the binary cache server https://cache.nixos.org/ ?
It looks like it will work without network connection so I'm curios how does it know about the available packages?

You have a local copy of nixpkgs (you can use nix-channel to manage it), nix-env should read from pkgs/top-level/all-packages.nix.
However you can override that position using nix-env -I "<nixpkgs_path> ...

Related

How can I create home-nix file from my current configuration

What I think that I've understood about home-manager.
Instead of using
nix-env iA packageToBeInstalled
you write a list of package in a file (/home/nixos/.config/nixpkgs/home.nix that I from now on call just home.nix)
you run
home-manager switch
and the package are installed.
But I have already installed some packages with nix-env. (home-manager for instance)
I would like to have my configuration save just in home.nix in order to just have to import it and execute home-manager switch to import the exact same configuration in a other OS.
Therefore I need a command replicating my configuration in home.nix.
I am not aware of a tool that fully automates this.
The nix-env list of installed packages is maintained in ~/.nix-profile/manifest.nix, but does not contain the attribute path, which is the "name" you need to use in such configuration files.
To find the attribute paths for the things you've installed, you may first create an index of sorts based on your current nixpkgs (NIX_PATH etc):
nix-env -qaP >packages.tmp
and then use it to look up each package.
Here's a one-liner that doesn't work for all packages.
nix-env -q | while read name; do grep "$name" <packages.tmp; done
So make sure to read through nix-env -q yourself and look up anything that's missing using for example https://search.nixos.org/packages.
To finalize, remove the imperatively installed packages with nix-env -e, check nix-env -q, and rm packages.tmp.

How to use `nix-env --set` without losing access to `nix-env`?

If I create a derivation using nix and then run nix-env --set <derivation name>, then commands like nix-env and nix are now missing from my nix profile and so I get the error Command 'nix-env' not found. I then have to mess around with symlinks in my nix profile to get it back.
This of course makes sense, because the nix package itself is not part of my derivation. Is there a way that I can use nix-env --set without losing access to nix itself?

What is the common way to install utility programs like file and ripgrep in NixOS

I want these programs to be installed in my user environment while not using nix-shell and nix-env (I was told to not use nix-env). I tried to use home-manager but I can't do program.file/ripgrep bc it's not an option. May I ask what is the common approach to install things like file and ripgrep in NixOS?
With home-manager, you can install programs by adding them by adding them in home.packages. For instance, if you want to install ripgrep, you could add in your home.nix:
home.packages = [
pkgs.ripgrep
];
Or, more conveniently
home.packages = with pkgs; [
ripgrep
];
You can add any program you want in that list.
Note that there is a difference between installing them with home-manager, and by adding it in environment.systemPackages, which is that the former will only install them for the user, while the latter will install system-wide. Besides that, both work similarly.
You should be able to set this in your nixos config:
environment.systemPackages = [
rigrep
file
];
I have no experience with home-manager so I don't know how it compares, but you should surely be able to install ripgrep with it.
I've been using nix-env for years without issue, only negatives is it's harder to keep track of things. But works fine for things you want to install 'temporarily' to have them available in your $PATH without having to invoke nix-shell.

How can I reset my nix environment to the original user profile?

I believe I ran nix-env -if example.nix which changed my nix environment.
How can I undo this action?
I'm trying to run a application that is specified in my nixos config (/etc/nixos/*), however it no longer seems available (within the $PATH).
Seems it might be nix-env --switch-profile /nix/var/nix/profiles/default (according to https://nixos.org/nix/manual/#sec-profiles) ?
I run nix-env -e '*' to remove all packages from my profile installed via nix-env regularly and move anything I want to use into environment.systemPackages so all my packages are tracked in my nixos configuration declaratively. As for debugging why the application specified in your configuration.nix isn't in your path, an application specified in your configuration.nix should be symlinked to /run/current-system/sw/bin, so the first thing would be to check that the binary you're looking for is listed in there and second that is in your $PATH.

How to get fully working grep in git bash (msysgit) on windows?

I would like to use grep -o, but in git bash there is no -o option. Is there a way to get full working grep in git bash, just like it's in linux bash shell?
There is no -o flag for grep
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/grep.html
You can use sed instead
There is an open issue for that on Github (even though it's under "nvm"). User UltCombo posted a workaround. Quoting:
Open <Git install directory>/bin and overwrite grep.exe with a more up to date version. I found two alternatives that provide -o support:
GnuWin32's grep 2.5.4 (link).
ezwinports' grep 2.10 (link). Note: You also have to extract libprce-0.dll in the same folder as grep.
Though ezwinports' grep port is much more up to date, I can't say whether any of these will cause stability/compatibility issues. I haven't found any issues yet, but use it at your own risk.
Marking this Community Wiki because it's really somebody else's work.
Alternatively, get the pretty awesome MSYS2 and enjoy full grep and co.

Resources