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

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.

Related

How to install the locate command on nixos

I've searched on the nixos package manager, if there is package named locate.
Yes and several. But that's not the bash command that I am searching for.
I want to add this bash command
locate name.extention
which gave me the path of this file
The package mlocate provides a locate executable. The package plocate provides an executable named plocate which might be better. Both of these packages will require some extra setup steps to allow them to build their file location database, and I don't happen to have a link to those instructions at the moment.
nix-shell -p mlocate

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?

Where does nix-env query for available packages?

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> ...

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.

Inject runtime dependency into nix package

Adding a runtime dependency to a package through override buildInputs causes the package to rebuild. Is there a simple way to inject runtime dependencies into a package without recompiling?
So basically adding package/bin to PATH and package/lib to LD_LIBRARY_PATH
If I understand correctly that you want to tweak the environment used when a Nix-installed app is run, not the one used when it is built, then a method I know of is as follows below. By using it, you essentially create a wrapper script, which overrides the "default command". So, something similar like creating e.g. a custom ~/bin/vim script, which adds some options/env overrides to the default vim binary, which is called with a "hardcoded original path" inside the script.
One example of it in nixpkgs is how vimutils.vimWithRC overrides vim command with a custom script. For your own use, you could write more or less something like below:
with import <nixpkgs> {};
writeScriptBin "vim" ''
#!/usr/bin/env bash
export PATH=package/bin:$PATH # whatever you like; I've added what you asked for
export LD_LIBRARY_PATH=package/lib:$LD_LIBRARY_PATH
${vim}/bin/vim --my-options "$#"
'';
If you put it in my-vim.nix, you should be able to install it with:
$ nix-env -e vim # REMOVE NORMAL VIM. I think this should be done first to avoid conflict
$ nix-env -i -f my-vim.nix
And hopefully it'll work and "override" the default vim for you.
DISCLAIMER: I haven't actually tested it in this exact form, sorry. Don't have a Nix console handy at this moment, unfortunately.

Resources