How to open a nix-shell session

Hello everyone,
This is my first post. I am very new to all the world of ubuntu and nix, and could barely understand what nix is but will be working on it.

I come here because in some installation guide it tells me “open a nix-shell session”

It looks to me that I installed nix correctly (got no error messages). But I tried for several hours to search online on how to open a nix-shell session and I just can’t find that out. When I type “nix-shell” on my command line in ubuntu it says “command not found”. That is why I come here for your help and thank you for anybody who can help me out on this.

What exactly are you trying to achieve (as in what’s your end goal)? Have you installed nix? Can you link the guide you’re following?

Given that you sound very new to Linux in general I don’t think that jumping straight into nix is a good idea if it can be avoided.

Hi, thank you for your answer.
Here’s the guide:
https://docs.cardano.org/projects/cardano-node/en/latest/getting-started/building-the-node-using-nix.html

Although even the first quadrant “./install-nix.sh” doesn’t work on my ubuntu 20 (“permission denied”, it even doesn’t work with “sudo”), but I managed to install nix using this guide until step 4: https://testnets.cardano.org/en/cardano/get-started/installing-and-running-the-cardano-node/building-the-node-using-nix/

And then on step 5. it’s the same, it says open a new nix-shell session, that I find impossible for me to understand how to do it.

Can you try running sudo rm -rf /nix, and installing nix again using the official nix documentation (https://nixos.org/download.html)? The installer also tells you that you have to source a particular shell script to get nix tools in your $PATH, so remember to do that when prompted. You should never run the installer as root, because then it’ll install a nix only usable by root. The installer script will prompt you for sudo access when needed.

Then you should have nix-shell in your $PATH and have it work.

It seems to somewhat work. I’ve created a new sudo user, installed nix according to the documentation within that user, but now when I type: “nix-shell” I get: error: getting status of shell.nix: Permission denied.
And if I type “sudo nix-shell” I get “sudo: nix-shell: command not found”, although this is weird cauz he recognised sudo before.

And I should always run the script that was prompted for me during installation before I type “nix-shell” on each login to get this error of “permission denied”. Otherwise he will not even find the command.

But at least now I have permission denied so I think the system is recognizing the function

By default, a non-nixos nix install is single-user, meaning that ONLY THE USER WHO INSTALLED NIX CAN USE IT. Not root, not any other user. That’s why running nix-shell with sudo won’t work (and is not needed).

Also, there’s absolutely no need to create a new user for nix. Just run the install as your user. Btw, that’s also probably why you’re getting permission denied: you’ve likely cloned the repo with user1's account, and are trying to access it with user2's (which you installed nix with). This won’t work because of permission issues.

1 Like

Ok, but the installation guide and you said I shouldn’t run the installation from root, so I should create a new user to install it with no?

From what I understand of what is written: I think that you need install nix with your CURRENT/NORMAL/MAIN Ubuntu user.

Thank you stphrolland for joining the conversation

but my normal ubuntu user is root. And on the installation page and here they say I shouldn’t do it on root

It is possible that this is only my opinion, but it is extremely uncommon, (and I have read also not recommended) that you should HAVE a root user login enabled in Ubuntu. Without knowing your reasons why you decided to modify Ubuntu so as to give login access to root user in your system: those reasons may very well be valid…

But if you plan to have a Ubuntu system with a root user, why don’t you simply use a Debian distribution ? Just a curiosity question. Ubuntu was specifically designed not to have a root user so as to simplify things for the mere mortals :slight_smile:

Correct me if I’m wrong, but simplifying things to the extreme, Ubuntu is roughly : Debian Linux Unstable + root user login removed + Default Graphic Environment + Proprietary/NonFree stuffs + details that makes them not totally transparently compatible.

That’s all I can add to the topic I think. Hope that helps.

1 Like

Ok thank you but all I’m trying to do is to install nix and run a nix-shell session on ubuntu, because this is how my vps works and I bought a new ubuntu computer.

You’re being really polite, I’m sorry you’re having such a hard time.
I’ll give you a bit of an intro since it seems you’re new to Linux

  • Basically every command you run (nix-shell, cd, even sudo) is a file somewhere.
  • Which files are used as commands? Well Ubuntu looks at your PATH variable to figure that out.
  • Running echo ${PATH} will show an (ugly) list of folders. (the :'s are used to separate each folder)
  • Ubuntu will treat every file in ANY of those folders as a command (even if its not supposed to be a command)
  • For example the /usr/bin/ folder should be in your PATH, and sudo is inside that folder.
  • (Running which sudo will tell you where that command is, ex: /usr/bin/sudo)
  • You can add new folders to your PATH
  • For example (do not do this): PATH="" would remove all the folders, making it where you had no commands at all (you would have to close that window and open a new one)
  • Different users can have different PATH’s (ex: the root user has a different PATH)
  • See this answer on how to change your path permanently

You might want to start by running whoami just to show us which user you’re executing this from.
Running chmod 'u+rwx' 'shell.nix' should give your current user (u+) all permissions (rwx read-write-execute) for the shell.nix file

Once your user has permission to use shell.nix, hopefully the command will work.

HOWEVER

  1. Nix should have changed your PATH automatically (might require restarting the terminal)
  2. Logging in as the root user when you don’t fully understand linux is a REALLY bad idea. Typing rm -r /as root will delete your entire file system.
  3. Many things (not just nix) don’t expect you to use root and they can break when you do

That guide you mentioned (docs.cardano.org), has a section that uses apt-get. I highly recommend creating a normal user and then using the apt-get instructions (for everything, not just docs.cardano.org) until you’re more familiar with Linux. Sadly, installing things on Linux isn’t always the most friendly and easy to use :confused: you’re going to have to do a lot of Googling. Watch as many video tutorials as possible. Nix is one of the hardest Linux tools to fully understand, but you came to the right place.

3 Likes

Don’t forget the heavy pushing of snap packages haha. Otherwise yeah, I’d say that+Brand-Recognition is 99% accurate.

2 Likes

Thank you Jeff-hykin for joining the conversation. I’ve taken notice of your post.
I am “root” (if I type whoami I get “root”). That is why I am stuck here, because I am not supposed to install nix as root. I know how to create a new user and install nix on it but the posts before told me I shoudn’t be creating a new user either…

As a test, I tried running nix-shell as root. It seems to work just fine on my NixOS machine. Granted, that’s a multi-user install of nix, but I’m thinking that a lot of this discussion of running as root is a bit of a red herring. The “sudo: nix-shell: command not found” message is the command sudo informing you that it can’t find the command nix-shell, which makes sense: sudo will likely have a different $PATH environment variable which does not contain the directory that includes nix-shell.

You mentioned that your username is root. To further confirm that you’re running as what I understand as root, what’s the user id? (you can see your user id with the id command, it’s the number in parenthesis after uid=, and root will usually have id 0)

Since you already have a user id setup for use with nix, you could change ownership of the project in question to that owner. chmod -R <user> <project-dir>, where you replace <user> with the nix username and <project-dir> with the location of the project directory in your filesystem (relative or absolute paths both work), should do the trick. Then you log into that user, navigate to the project directory (if you can’t navigate to it as the nix-user, you my have to move the project dir into the nix-user’s home dir), and run nix-shell as that user.

I hope that helps.

1 Like

Thank you theotherjimmy for joining the conversation. Ok I understand what you mean I might be able to change the ownership of the project. My user ID is 0 (it’s a VPS). Now I reinstalled my system and it’s a brand new one. So what you mean is that the owner should be a user other than root? In that case I should create a new user (I know how to do it). Right? I think I might have been mislead that I shouldn’t be creating a new user.
On the official Nix installation it says “other than root”

Ok I installed nix on a new user that I created (I suppose this is the way to go) and I ran this command and I get “permission denied”.

And also this one gives "chmod: invalid mode:

If I try to put “sudo” before these commands it says: cannot access “shell.nix”: no such file or directory. Although with find ~/nix.sh it gives me a bunch of files all of them are in the current user and at the end “find ‘nix.sh’: permission denied”

Yes that was misleading, basically everyone everywhere recommends having a non-root user.
Now I know what you mean by VPS, and yes for some reason they often come as root by default (I have one of my own like that).

The errors you’re getting are tough to debug without having access to the machine (and I don’t recommend giving access to anyone you’re not physically with)

All these “permission denied” and “no such file” error probably means something was fundamentally setup wrong. Even if you fix them (by finding it and giving yourself permission) its probably just going to result in new errors (or errors later when you try to use nix)

My best advice for nix is to completely uninstall it, all of it. It creates folders, files, and even users. You can find the list of all that from trying to install it (it tells you what it will change). Deleting all those manually might be hard, so if you just got the server, it might be easier to wipe the whole server.

Then, create a non-root user (e.g. “DrTwentyOne”), add “DrTwentyOne” to the sudoers group so it can use sudo, then login to that user account, and do a fresh install of nix from there.

I advise doing everything you can as a limited user, to contain any mistakes you make to things owned by that user. This limits your ability to, for example, remove the filesystem, which would make you computer not-bootable (I understand it’s a VPS, so perhaps it’s more illustrative to talk about getting support involved, or logging into the customer portal).

Based on the fact that you reinstalled, I’d say that the first thing you do is make a user account. Then login to that account and follow the instructions in the nix manual to install nix. After that, confirm that you can run nix-shell form that account. Then, with the account, clone the repo you’re trying to build. After that, run nix-shell, also from the same account.

What I’m trying to achieve, is setting up everything as a limited user account and to work in a way that’s many of us are more familiar with, that is to do everything with the user account. This will help with damage control, as I mentioned above, and maybe more importantly, it will make it much easier to follow most documentation, as it’s written from the perspective of a limited user account.

Fell free to call the user account whatever you like (except for root, that probably won’t work). I use “jimmy”, for example.

1 Like

I still have the same problem. I reinstalled ubuntu, created new user, gave it sudo power, even root powers via the “visudo” function then write ALL=(ALL) ALL and save it, copy pasted a command that the installation software prompted me to type at the end of the installation to “get the correct environment tools for nix”, and when I type “nix-shell” I get “permission denied”.
Although when I run echo ${PATH} I get “/home/DrTwentyOne/.nix-profile/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games”, seems like nix is in there. If I type sudo before “nix-shell” it says command not found.