Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

users.users.<name>.shell does not update the user's shell #811

Open
Samuel-Martineau opened this issue Nov 18, 2023 · 33 comments
Open

users.users.<name>.shell does not update the user's shell #811

Samuel-Martineau opened this issue Nov 18, 2023 · 33 comments

Comments

@Samuel-Martineau
Copy link

Samuel-Martineau commented Nov 18, 2023

For example, setting this in ~/.config/nix-darwin/flake.nix still requires running chsh -s /run/current-system/sw/bin/fish.

programs.fish.enable = true;

environment.shells = [pkgs.fish];

users.users.${mainUser} = {
  home = "/Users/${mainUser}";
  shell = pkgs.fish;
};

Also, should setting users.users.<name>.shell automatically add the shell to environment.shells?

@Samuel-Martineau
Copy link
Author

Closing as duplicate of #122

@Enzime Enzime changed the title users.users.<name>.shell does not update the user's shell users.users.<name>.shell does not update the user's shell Nov 20, 2023
@Enzime
Copy link
Collaborator

Enzime commented Nov 20, 2023

This looks to me like a separate to the one in #122

@Enzime Enzime reopened this Nov 20, 2023
@bestlem
Copy link

bestlem commented Nov 20, 2023

@Enzime I agree that this should be separate - the issue I think is that one very recent (out of the many ) comments there did put the two issues together and this is now seen - I think that it would be easier to see the issues to ignore that comment.

@Samuel-Martineau
Copy link
Author

@bestlem I am not even sure they are related anymore: even after manually changing my shell on my machine, the path issue was not fixed.

@bestlem
Copy link

bestlem commented Nov 20, 2023

@Samuel-Martineau That is our point they have always been two separate issues.

The comment I referred to has confused them and should be ignored. That thread deals with what should be in fish's startup files/

Your issue is about how to set fish as the login shell. (I thought this had been raised before)

@thuvasooriya
Copy link

What is the best way to resolve this for now? To set fish as the default login shell using nix?

@spcutrell
Copy link

Has this been resolved?

@Enzime
Copy link
Collaborator

Enzime commented Jul 9, 2024

This should’ve been fixed by #818

@Enzime Enzime closed this as completed Jul 9, 2024
@spcutrell
Copy link

This should’ve been fixed by #818

So now, in theory, I would just need:

programs.fish.enable = true;
users.users = {
  <name> = {
    home = "/Users/<name>";
    shell = pkgs.fish;
  };
};

and it should "just work"?

@Enzime
Copy link
Collaborator

Enzime commented Jul 9, 2024

Yeah that should work, feel free to comment if it doesn’t work

@spcutrell
Copy link

It definitely doesn't work for me. I see the correct location in /etc/shells at /run/current-systems/sw/bin/fish. There is a very real chance it is a GIGO issue. I pushed my proof-of-concept config if that is helpful to rule out User error.

https://github.com/spcutrell/nix/tree/main

@spcutrell
Copy link

spcutrell commented Jul 12, 2024

Hoping to reopen this issue. It doesn't seem fixed for me.

Steps to recreate:

  1. Fresh install on M1 (Sonoma 14.5)
  2. Nix installation using Determinate Installer
  3. Create ~/.config/nix/flake.nix (see below for content)
  4. Create ~/.config/nix/modules/darwin/default.nix (see below for content)
  5. Run nix run nix-darwin -- switch --flake ~/.config/nix/
  6. Run /run/current-system/sw/bin/darwin-rebuild switch --flake ~/.config/nix for good measure
  7. Reboot for good measure
{
  # ~/.config/nix/flake.nix
  description = "Entry flake";

  inputs = {
    nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";

    darwin.url = "github:LnL7/nix-darwin";
    darwin.inputs.nixpkgs.follows = "nixpkgs";
  };

  outputs = inputs@{ self, nixpkgs, darwin, home-manager, ... }: {
    darwinConfigurations."My-MacBook-Air" = darwin.lib.darwinSystem {
      system = "aarch64-darwin";
      pkgs = import nixpkgs {
        system = "aarch64-darwin";
        config.allowUnfree = true;
      };
      modules = [ ./modules/darwin ];
    };
  };
}
{ pkgs, ... } : {
  # ~/.config/nix/modules/darwin/default.nix
  programs.fish.enable = true;
  
  environment.shells = [pkgs.fish];

  users.users.myUser = {
    home = /Users/myUser;
    shell = pkgs.fish;
  };
  nix.extraOptions = ''
    experimental-features = nix-command flakes
  '';
  services.nix-daemon.enable = true;
  system.stateVersion = 4;
}

cat /etc/shells to prove that the build is working:

# Ftpd will not allow users to connect who are not using
# one of these shells.

/bin/bash
/bin/csh
/bin/dash
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh

# List of shells managed by nix.
/run/current-system/sw/bin/fish

Now from here, I am not really sure what I expect. However, opening up terminal is still zsh and echo $SHELL still shows /bin/zsh and chsh -s /run/current-system/sw/bin/fish correctly sets shell to fish as expected (showing that nix properly installed it).

@Enzime
Copy link
Collaborator

Enzime commented Jul 13, 2024

I don't have the time to investigate and fix this now, I'll just leave some notes here for anyone who wants to test it out and make a PR

I suspect it'll have something to do with this command

dscl . -create '/Users/${v.name}' UserShell ${lib.escapeShellArg (shellPath v.shell)}

Possibly -create doesn't work if shell is already defined and we might need to use a different flag instead for dscl

@Enzime Enzime reopened this Jul 13, 2024
@z0al
Copy link

z0al commented Jul 14, 2024

Possibly -create doesn't work if shell is already defined and we might need to use a different flag instead for dscl

@Enzime when I run the same command manually it works fine even if the shell is already set

sudo dscl . -create /Users/me UserShell /run/current-system/sw/bin/fish

I tried to to debug the path being set by adding an echo I don't see it logged any where during system activation. What am I missing?

@Enzime
Copy link
Collaborator

Enzime commented Jul 14, 2024

I think the only way that code doesn’t run is if your user’s uid doesn’t match the one set in nix-darwin (I think the default is 501), if that’s the case a warning should be emitted

@z0al
Copy link

z0al commented Jul 14, 2024

My user ID is 501 🤔

$ dscl . -read /Users/z0al UniqueID
UniqueID: 501

@Enzime
Copy link
Collaborator

Enzime commented Jul 14, 2024

Can you test if the dscl command can change your shell to zsh or bash? You might need to restart before the change is reflected

@z0al
Copy link

z0al commented Jul 14, 2024

Yep, I did that and it works without restarting.

$ dscl . -read /Users/z0al UserShell
UserShell: /run/current-system/sw/bin/fish

$ sudo dscl . -create /Users/z0al UserShell /run/current-system/sw/bin/bash

$ dscl . -read /Users/z0al UserShell
UserShell: /run/current-system/sw/bin/bash

$ sudo dscl . -create /Users/z0al UserShell /run/current-system/sw/bin/fish

$ dscl . -read /Users/z0al UserShell
UserShell: /run/current-system/sw/bin/fish

@spcutrell
Copy link

spcutrell commented Jul 14, 2024

To throw a wrinkle into this, my shell doesn't change using the dscl command. UserId is 501.

(correction)

@z0al
Copy link

z0al commented Jul 14, 2024

That's weird. In my case, this works just fine. Can you give it a shot @spcutrell ?

system.activationScripts.setFishAsShell.text = ''
  dscl . -create /Users/${user} UserShell /run/current-system/sw/bin/fish
'';

@spcutrell
Copy link

Well... forget what I posted. It is working now. I thought I was pretty thorough, but I'm going to chalk this up to user error for now.

The dscl . -create command works.

@z0al
Copy link

z0al commented Jul 14, 2024

I figured it out (at least for my own setup). As I mentioned earlier, the activation script doesn't run at all since I don't see logs e.g setting up users

Looking at the script, it's behind a condition mkIf (cfg.knownUsers != []) 🤦🏻‍♂️ . So the fix was as simple as adding this to my config:

users.knownUsers = [ user ];
users.users.${user}.uid = 501; # or whatever your user id is

And boom! I now see the logs and the shell is correctly set up 🎉

Edit: I think nix-darwin should warn users if the list of knownUsers is empty. I didn't know this config even existed and could easily miss it even if I did.

@spcutrell
Copy link

Confirming that it works for me as well.

I'm not sure what mac views as "admin", but is there any concern about this from the docs?

users.knownUsers
List of users owned and managed by nix-darwin. Used to indicate what users are safe to create/delete based on the configuration. Don’t add the admin user or other system users to this.

@z0al
Copy link

z0al commented Jul 14, 2024

I'm not sure what mac views as "admin", but is there any concern about this from the docs?

I'm also not sure. My user is definitely an admin and I'd expect every other single-user setups to have the same issue.

@spcutrell
Copy link

Well in any case, great work and thanks for your help. Not sure if this is expected behavior or how the maintainers would like to proceed, but it is pseudo-resolved imo.

Side note: I decided I would learn Nix by just slowly building out a nix-darwin set-up, and my first goal was to get the shell changed from zsh... didn't realize I stepped into a trap!

@z0al
Copy link

z0al commented Jul 14, 2024

Funnily enough, I also stumbled across the same issue last year while building my nix-darwin/nixos setup. I made a home-manager script that calls chsh -s /path/to/fish at the time and it worked just fine.

Today, however, I was doing a major refactoring to my setup and and decided to check if the issue is still open 😄

@Samasaur1
Copy link
Contributor

I wasn't around when this option was created, so I don't know for sure, but my instinct is that that warning is there because letting nix-darwin manage your primary/admin user could potentially really screw up your system. I would strongly recommend not putting your user in users.knownUsers and would instead run dscl . -create /Users/${user} UserShell /run/current-system/sw/bin/fish or chsh -s /run/current-system/sw/bin/fish in an activation script

@z0al
Copy link

z0al commented Jul 15, 2024

It's not just about the shell though, most if not all of the options under users.users.<user> are set within that same script so that renders all of them useless?

@emilazy
Copy link
Collaborator

emilazy commented Jul 15, 2024

Yes, I think the status quo is that you shouldn’t use the users.users.* arguments on your main user, but frankly I forget why.

@z0al
Copy link

z0al commented Jul 15, 2024

you shouldn’t use the users.users.* arguments on your main user

Coming from NixOS background that's quite the surprise frankly.

I suggest making that more prominent in the official docs to avoid confusion

@emilazy
Copy link
Collaborator

emilazy commented Jul 15, 2024

We could maybe just remove the warning. It seems like the worry was that removing your user config would delete your user, but you can’t actually delete the currently logged‐in user anyway.

@z0al
Copy link

z0al commented Jul 15, 2024

It seems like the worry was that removing your user config would delete your user,

I'd expect nix-darwin to manage the user if specified in users.users.* and ignore it if it's not there by default. That's at least the default behavior in NixOS AFAIK unless you explicity set users.mutableUsers to false

lentil32 added a commit to lentil32/dotfiles-nix that referenced this issue Aug 9, 2024
lentil32 added a commit to lentil32/dotfiles-nix that referenced this issue Aug 9, 2024
atahanyorganci added a commit to atahanyorganci/setup that referenced this issue Aug 28, 2024
This commit fixes the issue where settings `users.user.${user}.shell` wouldn't change default shell for the user.

This was long running issue with `nix-darwin` and it's finally resolved upstream. [LnL7/nix-darwin#811][issuse] details how this was resolved.

[issuse]: LnL7/nix-darwin#811
@kblcuk
Copy link

kblcuk commented Sep 21, 2024

Fwiw I had the same problem, and it might be that for me this was somehow caused by homebrew installation of fish. Which, after uninstalling it, didn't clear the UserShell:

❯ dscl . -read /Users/kblcuk UserShell
UserShell: /opt/homebrew/bin/fish

~/nix-darwin-config on ☁️  (eu-central-1)
❯ sudo dscl . -create /Users/kblcuk UserShell /run/current-system/sw/bin/fish

~/nix-darwin-config on ☁️  (eu-central-1)
❯ dscl . -read /Users/kblcuk UserShell
UserShell: /run/current-system/sw/bin/fish

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants