How to open a nix-shell session

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.

I understand that’s quite frustrating. I agree, that seems like nix-shell is not there. nix-shell is provided by the same derivation as nix and nix-env on my system, so it seems like something has gone wrong in an unusual way, leading to either the nix derivation to be missing or to the executables provided are not valid for some reason. This leads me to have more questions

  1. What’s in the ~/.nix-profile/bin directory? (just in case ~ means home directory, or /home/DrTwentyOne for example)
  2. What version of Ubuntu are you using?
  3. What architecture are you using (uname -p will give you this info)?
  4. Is the /nix directory available?
  5. is the nix executable available somewhere in the /nix/store directory (find /nix/store -name nix can help with this)
2 Likes

Yeah a bit frustrating but I’m in no hurry and generally very patient :blush:
1.nix nix-collect-garbage nix-env nix-prefetch-url
nix-build nix-copy-closure nix-hash nix-shell
nix-channel nix-daemon nix-instantiate nix-store

  1. Ubuntu 20

  2. x86_64

  3. Yes, it has “store var” in it.

  4. If I type “find /nix/store -name nix” I get a big set of lines that looks like " /nix/store/4vz8sh9ngx34ivi0bw5hlycxdhvy5hvz-nix-2.3.7/share/doc/nix"
    and at the end I get “find: Failed to restore initial working directory: /root: Permission denied”

And by the way, if I copy paste in this user the command I find on the Cardano website to install nix (curl https://nixos.org/nix/install > install-nix.sh), I also get permission denied. But when I install it with the command line given by the official nix website (curl -L https://nixos.org/nix/install | sh) it works. I think there must be something seriously wrong with permission issues.

Okay, so it looks to me like nix managed to install itself, but the executables that it installed and available on the path, but are not possible to run. I’m not as familiar with how to debug this, but I have a few ideas.

I usually find that executable issues like this come up for a few reasons:

  1. Incorrect binary architecture or format. You can inspect this with some bash like:
 ➜ file $(readlink -f $(which nix))
/nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/bin/nix: ELF 64-bit LSB executable, x86-64, version 1 (GNU/Linux), dynamically linked, interpreter /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=e8534c26cadeebff98a2ea0ed2630ccd2f913b37, not stripped

We’re specifically looking for it to say ELF 64-bit LSB executable and mention Linux as well.
2. The executable bit is not set. We can check this in bash as well:

➜ stat $(readlink -f $(which nix))
  File: /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/bin/nix
  Size: 1816736   	Blocks: 3552       IO Block: 4096   regular file
Device: 1ch/28d	Inode: 15791231    Links: 2
Access: (0555/-r-xr-xr-x)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2020-07-21 15:57:20.038455017 -0500
Modify: 1969-12-31 18:00:01.000000000 -0600
Change: 2020-06-05 13:13:19.346856373 -0500
 Birth: 2020-04-20 08:34:56.949489056 -0500

Here, we look at the second field within the first access member, looking for the “r” and “x” characters. Note that the permissions are duplicated 3 times; once for permissions of the file owner (root in this case), once for the group of the file (also root), and once for all other user accounts.
3. Shared libraries are missing. We can check this with bash too:

➜ ldd $(which nix)
	linux-vdso.so.1 (0x00007ffeb4bf8000)
	libsodium.so.23 => /nix/store/83a0cva7q7jgf303yv466n6v8rhx6isd-libsodium-1.0.18/lib/libsodium.so.23 (0x00007f16759f2000)
	libeditline.so.1 => /nix/store/cv6lyjsc721nv34g1nnb50zfd8zbj6xd-editline-1.17.0/lib/libeditline.so.1 (0x00007f16759e6000)
	libboost_context.so.1.69.0 => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libboost_context.so.1.69.0 (0x00007f16759e0000)
	libboost_thread.so.1.69.0 => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libboost_thread.so.1.69.0 (0x00007f16759b2000)
	libboost_system.so.1.69.0 => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libboost_system.so.1.69.0 (0x00007f16759ad000)
	libnixexpr.so => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libnixexpr.so (0x00007f1675864000)
	libgc.so.1 => /nix/store/phvdmwbnqmi8ibjsyzadjw6lblfscrsg-boehm-gc-8.0.4/lib/libgc.so.1 (0x00007f16755e2000)
	libnixmain.so => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libnixmain.so (0x00007f16755ae000)
	libnixstore.so => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libnixstore.so (0x00007f167535d000)
	libnixutil.so => /nix/store/vfahm96nmi8layj9cjwnv3w57xdf3jb4-nix-2.3.4/lib/libnixutil.so (0x00007f16752a6000)
	libstdc++.so.6 => /nix/store/jy89v2q2zv074mvw91jgqcvkmk7yqx69-gcc-9.3.0-lib/lib/libstdc++.so.6 (0x00007f16750c5000)
	libm.so.6 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libm.so.6 (0x00007f1674f83000)
	libgcc_s.so.1 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libgcc_s.so.1 (0x00007f1674f69000)
	libpthread.so.0 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libpthread.so.0 (0x00007f1674f48000)
	libc.so.6 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libc.so.6 (0x00007f1674d89000)
	/nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/ld-linux-x86-64.so.2 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib64/ld-linux-x86-64.so.2 (0x00007f1675a50000)
	librt.so.1 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/librt.so.1 (0x00007f1674d7f000)
	libdl.so.2 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libdl.so.2 (0x00007f1674d78000)
	libcrypto.so.1.1 => /nix/store/g39cbah5km1rm4x0b884z2zr6ihy1p4b-openssl-1.1.1d/lib/libcrypto.so.1.1 (0x00007f1674a92000)
	libsqlite3.so.0 => /nix/store/d107q6qzjb8a7zxng5ipnmhzhjsa63v4-sqlite-3.31.1/lib/libsqlite3.so.0 (0x00007f1674970000)
	libbz2.so.1 => /nix/store/66bmzz2vhnrqwbgpp7mki4dbvqgyjhz3-bzip2-1.0.6.0.1/lib/libbz2.so.1 (0x00007f167495b000)
	libcurl.so.4 => /nix/store/8zxiz4i59h1c7qfk5zg23x1bfzzyykiq-curl-7.68.0/lib/libcurl.so.4 (0x00007f16748cd000)
	libaws-cpp-sdk-transfer.so => /nix/store/9v1c73ad4hb75mhqxs38hw6z92x4hdb8-aws-sdk-cpp-1.7.90/lib/libaws-cpp-sdk-transfer.so (0x00007f167489a000)
	libaws-cpp-sdk-s3.so => /nix/store/9v1c73ad4hb75mhqxs38hw6z92x4hdb8-aws-sdk-cpp-1.7.90/lib/libaws-cpp-sdk-s3.so (0x00007f16745c2000)
	libaws-cpp-sdk-core.so => /nix/store/9v1c73ad4hb75mhqxs38hw6z92x4hdb8-aws-sdk-cpp-1.7.90/lib/libaws-cpp-sdk-core.so (0x00007f1674499000)
	libseccomp.so.2 => /nix/store/pj478xk8048jasr40rji7ba86qwg4q3g-libseccomp-2.4.3-lib/lib/libseccomp.so.2 (0x00007f167444a000)
	liblzma.so.5 => /nix/store/zf40yxz49x74j777pn6hgk11xw0mgcv8-xz-5.2.5/lib/liblzma.so.5 (0x00007f1674420000)
	libbrotlienc.so.1 => /nix/store/mnva81a151jssss3npr3kc948qml9jja-brotli-1.0.7-lib/lib/libbrotlienc.so.1 (0x00007f1674386000)
	libbrotlidec.so.1 => /nix/store/mnva81a151jssss3npr3kc948qml9jja-brotli-1.0.7-lib/lib/libbrotlidec.so.1 (0x00007f1674377000)
	libz.so.1 => /nix/store/zsr1dksfh97yvb49k3rni00q5lppd9pd-zlib-1.2.11/lib/libz.so.1 (0x00007f167435a000)
	libnghttp2.so.14 => /nix/store/5qqcybnrmy01ddwmx9dym1qxkc7ivncn-nghttp2-1.40.0-lib/lib/libnghttp2.so.14 (0x00007f167432f000)
	libssh2.so.1 => /nix/store/537b2ywpxmshxk9q58pc1770bjnk1syq-libssh2-1.9.0/lib/libssh2.so.1 (0x00007f16742ef000)
	libssl.so.1.1 => /nix/store/g39cbah5km1rm4x0b884z2zr6ihy1p4b-openssl-1.1.1d/lib/libssl.so.1.1 (0x00007f1674259000)
	libgssapi_krb5.so.2 => /nix/store/h3jypkrycrji4dn77472slapvsswpk5v-libkrb5-1.18/lib/libgssapi_krb5.so.2 (0x00007f1674206000)
	libkrb5.so.3 => /nix/store/h3jypkrycrji4dn77472slapvsswpk5v-libkrb5-1.18/lib/libkrb5.so.3 (0x00007f167412a000)
	libk5crypto.so.3 => /nix/store/h3jypkrycrji4dn77472slapvsswpk5v-libkrb5-1.18/lib/libk5crypto.so.3 (0x00007f16740fa000)
	libcom_err.so.3 => /nix/store/h3jypkrycrji4dn77472slapvsswpk5v-libkrb5-1.18/lib/libcom_err.so.3 (0x00007f16740f4000)
	libaws-c-event-stream.so.0unstable => /nix/store/ycj4pxkzw29g41l9w8bfnvlsfafp6g26-aws-c-event-stream-0.1.1/lib/libaws-c-event-stream.so.0unstable (0x00007f16740e2000)
	libaws-checksums.so => /nix/store/ppbdv2jvh5dvlwsqpipgy9fbjff54hh7-aws-checksums-0.1.5/lib/libaws-checksums.so (0x00007f16740d4000)
	libbrotlicommon.so.1 => /nix/store/mnva81a151jssss3npr3kc948qml9jja-brotli-1.0.7-lib/lib/libbrotlicommon.so.1 (0x00007f16740af000)
	libkrb5support.so.0 => /nix/store/h3jypkrycrji4dn77472slapvsswpk5v-libkrb5-1.18/lib/libkrb5support.so.0 (0x00007f16740a0000)
	libkeyutils.so.1 => /nix/store/d3n1a9hv1vzlbvvv2fhg1p3asclmy4xh-keyutils-1.6.1-lib/lib/libkeyutils.so.1 (0x00007f1674099000)
	libresolv.so.2 => /nix/store/an6bdv4phxsz14q2sk57iscl2dc7bnj1-glibc-2.30/lib/libresolv.so.2 (0x00007f1674080000)

In this, we’re looking for any line that is missing a fle path. If it’s missing any of these deps, we probably have to add that dep to ubuntu. (at least temporarily, as nix should be able to install all of it’s own deps).