Giter VIP home page Giter VIP logo

Comments (8)

dustinlyons avatar dustinlyons commented on May 2, 2024

Try this

nix shell nixpkgs#<package name>

from nixos-config.

dustinlyons avatar dustinlyons commented on May 2, 2024

Nix is so damn confusing 😆

When you see any Nix command with a - in it, like nix-env, nix-shell, that's "legacy" and using Nix channels.

Anything with a space, like nix shell, is the "new" way and uses flakes.

https://nixos.org/manual/nix/stable/command-ref/new-cli/nix3-shell

I dropped all usage of channels in this project, so it's flakes only.

from nixos-config.

tlindsay avatar tlindsay commented on May 2, 2024

That works, but only for the shell in which it's run. I use tmux a lot and am constantly spawning/killing shell sessions as part of the same workflow. Is there no way to install an ad-hoc package to a profile?

from nixos-config.

dustinlyons avatar dustinlyons commented on May 2, 2024

I kind of skipped the whole Nix profile feature in this project, as I felt it was somewhat overkill. Here's my workflow:

  1. Use nix shell for things I want to try out quickly
  2. If I like it and want to use it, just add the package to my configuration if it's something I want to stick around. Then nix run .#build-switch.

Most of my packages end up in shared:
https://github.com/dustinlyons/nixos-config/blob/main/modules/shared/packages.nix

But for Darwin specific, I put them here:
https://github.com/dustinlyons/nixos-config/blob/main/modules/darwin/packages.nix

It's really easy to add and remove them without any consequence. From my understanding, profiles just give you the ability to rotate between different sets of packages in your environment, which I don't really use.

As long as you don't update your flake.lock file, and just add a package to your configuration, it's very fast to install it (~same speed as profile or nix shell). It's only when you run nix flake update that it takes a while (as it's rebuilding all new updates).

from nixos-config.

dustinlyons avatar dustinlyons commented on May 2, 2024

Hi @tlindsay, curious if the above workflow is sufficient for your use case. I'd just install tmux as part of your configuration if you use it all the time (it's what I do).

from nixos-config.

tlindsay avatar tlindsay commented on May 2, 2024

tmux is installed as part of my configuration, that's not really the issue. The issue is that I lose access to whatever command I need from $ nix shell ... as soon as I drop into a new shell (which I do frequently using tmux).

It would be nice to be able to install something and have it persist until I generate a new generation from my flake. That lets me try things out, but have them automatically garbage collected whenever I rebuild my flake. If I need something past that point, it tells me something about how useful that tool actually is, and whether it deserves to become a permanent addition.

(Also, sorry for the long delay between responses. I need to tame my Github notification settings 😅)

from nixos-config.

dustinlyons avatar dustinlyons commented on May 2, 2024

Okay, I understand now.

When nix shell nixpkgs#hello runs, all it's doing is looking up if you have hello in the Nix store, then if you don't, building it, then dropping you in a shell and adding a path like this to your PATH variable:

/nix/store/h92a9jd0lhhniv2q417hpwszd4jhys7q-hello-2.12.1/bin

So nothing is ever really installed, it's just sitting in the Nix Store. Your PATH is the only thing that is mutated.

You may want to look at nix-direnv, or for more complex projects nix-direnv + devenv. It doesn't exactly meet your use case (installing temporary packages with nix shell and having them available everywhere), but it automatically makes packages available to you when you drop into a particular directory. So, if you're trying project-specific stuff, you could edit that file and not touch your main configuration. I do this for most projects I'm working on.

Otherwise, you're looking at a script that exports your PATH from within the Nix shell and imports it back in your regular shell.

You could do something like this:

Add to your .bashrc/.zshrc:

function export_nix_path_and_exit() {
  if [ -n "$IN_NIX_SHELL" ]; then
    echo $PATH > /tmp/nix_path_export
  fi
  exit
}

trap export_nix_path_and_exit EXIT

function check_nix_shell_exit() {
  if [ -n "$IN_NIX_SHELL" ]; then
    # Inside Nix shell, do nothing
    return
  fi

  if [ -f /tmp/nix_path_export ]; then
    export PATH=$(cat /tmp/nix_path_export)
  fi
}

PROMPT_COMMAND="check_nix_shell_exit; $PROMPT_COMMAND" (for `bash`)
precmd_functions+=(check_nix_shell_exit) (for `zsh`)

This says:

  • on shell exit, check if I'm in a nix shell, and if I am, export my PATH
  • on entering any shell, check if I've exported a nix shell PATH, and if I have, override my current PATH

To reset/clear it, you can just delete the /tmp/nix_path_export file. This could be something you do on every rebuild with a script.

from nixos-config.

dustinlyons avatar dustinlyons commented on May 2, 2024

Hi @tlindsay, I'm spending some time cleaning up Github Issues. I hope the answer above helps in some way. Let me know here (on this Issue) if you have any more questions.

from nixos-config.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.