Revisionless path to mounted snap package resource?

I want to chsh --shell "$PowerShell" (to set PowerShell as my default shell) where $PowerShell is the revisionless path to pwsh in $PATH. I could use env for this, but I expect there’s a reason why most distributions (which set bash as their default) don’t use env.

In PowerShell, Get-Command 'pwsh' and [System.Diagnostics.Process]::GetCurrentProcess().MainModule.FileName both return /var/lib/snapd/snap/powershell/267/opt/powershell/pwsh. This isn’t usable, because it’ll break when the package is updated.

You can use ‘current’ symlink instead of specific revision, that shouldn’t break :slight_smile:

1 Like

@KhazAkar, like chsh -s '/var/lib/snapd/snap/powershell/current/opt/powershell/pwsh'? Many thanks.

Like chsh -s '/snap/powershell/current/opt/powershell/pwsh' , try that one :slight_smile:

1 Like
PS /home/RokeJulianLockhart> chsh -s '/snap/powershell/current/opt/powershell/pwsh'              
Changing shell for RokeJulianLockhart.
Password: 
chsh: "/snap/powershell/current/opt/powershell/pwsh" is not listed in /etc/shells.
Use chsh -l to see list.
PS /home/RokeJulianLockhart> cat -vbET '/etc/shells'                               
     1  /bin/sh$
     2  /bin/bash$
     3  /usr/bin/sh$
     4  /usr/bin/bash$
     5  /usr/bin/tmux$
     6  /bin/tmux$
PS /home/RokeJulianLockhart>

@KhazAkar, perhaps my method is fundamentally flawed, actually. I thought that it would work. /var/lib/snapd/snap/powershell/current/opt/powershell/pwsh doesn’t work either.

What distro do you use? I’m on Ubuntu 24.04 which creates /snap symlink :slight_smile:

Plus, this shell needs to be added to /etc/shells to be recognized

@KhazAkar,

I’m using https://download.fedoraproject.org/pub/fedora/linux/releases/40/Spins/x86_64/iso/Fedora-KDE-Live-x86_64-40-1.14.iso.

It does appear to exist:

PS /home/RokeJulianLockhart> Get-ChildItem -LiteralPath '/snap/'

    Directory: /snap

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
drwxr-xr-x       root root       13/04/2024 17:35           16 bare
drwxr-xr-x       root root       29/05/2024 10:37           46 bin
drwxr-xr-x       root root       13/04/2024 18:02           24 core
drwxr-xr-x       root root       17/05/2024 20:59           30 core20
drwxr-xr-x       root root       01/05/2024 23:37           30 core22
drwxr-xr-x       root root       13/04/2024 17:36           22 gtk-common-themes
drwxr-xr-x       root root       29/05/2024 10:37           26 powershell
drwxr-xr-x       root root       23/05/2024 15:18           34 snapd
drwxr-xr-x       root root       13/04/2024 18:02           16 what-snap
-r--r--r--       root root       12/04/2024 17:38          590 README

PS /home/RokeJulianLockhart>

Thanks. I’ll do that and try again.

PS /home/RokeJulianLockhart> chsh -s '/snap/powershell/current/opt/powershell/pwsh'
Changing shell for RokeJulianLockhart.
Password: 
Shell changed.

Many thanks.

1 Like

Interesting, so fedora did this: ln -s /var/lib/snapd/snap /snap

I will spin VM with it and check myself out.

1 Like

You’re welcome! Do not hesitate to ask on the forum if something is bothering you about snaps :slight_smile:

1 Like

@KhazAkar, does that differ to Ubuntu?

Not in majorly different way, usually distros don’t ship more functioning snapd package than bare minimum, so it’s a welcoming suprise :slight_smile:

1 Like

Yup. Fedora doesn’t do the symlink. But anyway, check the path for powershell. Run this to get the path

which powershell

Then, run

chsh --shell /path/to/powershell

In most cases it should be /snap/bin/powershell

but because fedora doesn’t have the symlink by default, this path might also work /var/lib/snapd/snap/bin/powershell

1 Like

@soumyaDghosh,

That’s equivalent to the aforestated Get-Command and command -v:

PS /home/RokeJulianLockhart> Get-Command 'pwsh'                 

CommandType     Name                                               Version    Source
-----------     ----                                               -------    ------
Application     pwsh                                               0.0.0.0    /var/lib/snapd/snap/powershell/267/opt/powershell/pwsh

PS /home/RokeJulianLockhart> which 'pwsh'      
/var/lib/snapd/snap/powershell/267/opt/powershell/pwsh
PS /home/RokeJulianLockhart> command -v 'pwsh' 
/var/lib/snapd/snap/powershell/267/opt/powershell/pwsh

In fact, I’ve seen at https://stackoverflow.com/revisions/677212/17#:~:text=Avoid%20which.%20Not%20only%20is%20it%20an%20external%20process%20you’re%20launching%20for%20doing%20very%20little%20(meaning%20builtins%20like%20hash%2C%20type%20or%20command%20are%20way%20cheaper)%2C%20you%20can%20also%20rely%20on%20the%20builtins%20to%20actually%20do%20what%20you%20want%2C%20while%20the%20effects%20of%20external%20commands%20can%20easily%20vary%20from%20system%20to%20system that command -v is preferable (in a POSIX environment) whereas I otherwise use Get-Command because it returns (JSON) objects.

I’m not sure I’ve ever seen /snap/bin/ be used, although it certainly exists with a powershell directory inside:

PS /home/RokeJulianLockhart> Get-ChildItem -LiteralPath '/snap/bin/'

    Directory: /snap/bin

UnixMode         User Group         LastWriteTime         Size Name
--------         ---- -----         -------------         ---- ----
lrwxrwxrwx       root root       29/05/2024 10:37           13 powershell -> /usr/bin/snap
lrwxrwxrwx       root root       29/05/2024 10:37           10 pwsh -> powershell
lrwxrwxrwx       root root       13/04/2024 18:02           13 what-snap -> /usr/bin/snap

How does it differ? Merely /snap/ worked quite well - I used chsh --shell '/snap/powershell/current/opt/powershell/pwsh', and it’s functioning.

You should never invoke a binary directly from within a snap squashfs! Can you kindly print the output of

which powershell
2 Likes

which pwsh, since binary is pwsh, not powershell

Nope not pwsh, that binary isn’t directly exposed by this snap. powershell is the binary that’s exposed. And setting cshs --shell /snap/bin/powershell worked.

2 Likes

Thanks for that! Simpler path than previous solution :slight_smile:

1 Like

@soumyaDghosh,

/var/lib/snapd/snap/bin/powershell. Should that be set as my shell path instead?

Why? Because there’s no guarantee that it’ll be available? I suppose that that would apply to powershell too though, since (I expect) it’s just a symbolic link to the pwsh binary inside the SquashFS FS?

Because snap doesn’t run the executables directly, powershell in /snap/bin/powershell is actually a symlink to snapd itself, which effectively runs snap run powershell and sets up the environment.

It’s more likely to apply to strict snaps due to the additional layers involved, but even in classic, you’re bypassing wrappers such as setting the $SNAP variables apps might expect to actually function (E.G, if a script points to $SNAP/bin/executable, it might instead accidentally try /bin/executable which may not exist or be compatible).

2 Likes