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

Add option to enable the service for all users #83

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ you'll have to manually enable the service for each user (see below).

#### Enable the service

##### Automatically for all users

Instead of just
```nix
{ services.vscode-server.enable = true; }
```

use:
```nix
{
services.vscode-server = {
enable = true;
enableForUsers.enable = true;
};
}
```

This will use `tmpfiles` to setup the permanent symlink described below for each regular user.
Ten0 marked this conversation as resolved.
Show resolved Hide resolved

If you do not wish to enable it for all users, but only for a specific subset, the list of users this will be setup for can be overridden:

```nix
{
services.vscode-server.enableForUsers.users = [ "alice" "bob" ];
}
```

Note that when disabling `services.vscode-server.enableForUsers.enable`, the file that was created in the user's `.config/systemd/user` will not be cleaned up, so you will have to clean it up manually.

##### Manually for each user

And then enable them for the relevant users:

```bash
Expand Down Expand Up @@ -79,6 +110,8 @@ ln -sfT /run/current-system/etc/systemd/user/auto-fix-vscode-server.service ~/.c

### Home Manager

#### Install as a tarball

Put this code into your [home-manager](https://github.com/nix-community/home-manager) configuration i.e. in `~/.config/nixpkgs/home.nix`:

```nix
Expand All @@ -91,6 +124,25 @@ Put this code into your [home-manager](https://github.com/nix-community/home-man
}
```

#### Install as a flake

```nix
{
inputs.vscode-server.url = "github:nix-community/nixos-vscode-server";

outputs = { self, vscode-server, home-manager }: {
homeConfigurations.yourhostname = home-manager.lib.homeManagerConfiguration {
modules = [
vscode-server.homeModules.default
({ config, pkgs, ... }: {
services.vscode-server.enable = true;
})
];
};
};
}
```

## Usage

When using VS Code as released by Microsoft without any special needs, just enabling and starting the service should be enough to make things work. If you have some custom build or needs, there are a few options available that might help you out.
Expand Down
65 changes: 55 additions & 10 deletions modules/vscode-server/default.nix
Original file line number Diff line number Diff line change
@@ -1,10 +1,55 @@
import ./module.nix ({
name,
description,
serviceConfig,
}: {
systemd.user.services.${name} = {
inherit description serviceConfig;
wantedBy = [ "default.target" ];
};
})
import ./module.nix (
{ name
, description
, serviceConfig
, lib
, config
, cfg
}: lib.mkMerge [
{
systemd.user.services.${name} = {
inherit description serviceConfig;
wantedBy = [ "default.target" ];
};
}
(lib.mkIf cfg.enableForUsers.enable {
systemd.tmpfiles.settings =
let
forEachUser = ({ path, file }: builtins.listToAttrs
(builtins.map
(username: let user = config.users.users.${username}; in {
Copy link

@fsnkty fsnkty Oct 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still unable to get this to work as expected both config.users.users.foo.name and "bar" fail.
when e.g.. config.users.users.foo.name = "bar"
for reference users.groups.<name>.members handles this correctly, may be useful to take a look at that.
thank you

Copy link
Collaborator Author

@Ten0 Ten0 Oct 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please expand on how it "doesn't work as expected"?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please expand on how it "doesn't work as expected"?

I have config.users.users.main.name = "fsnkty"
setting services.vscode-server.enableForUsers.users = [ config.users.users.main.name ];
breaks this in that

       error: attribute 'fsnkty' missing
       at /nix/store/fbgf31l3gy506di92bbw12aw95jg4cs6-source/modules/vscode-server/default.nix:20:37:
           19|             (builtins.map
           20|               (username: let user = config.users.users.${username}; in {
             |                                     ^
           21|                 name = "${user.home}/${path}";

giving "fsnkty" breaks in the same way, the string "main" can be used but that's non ideal and doesn't behave similarly to any other option that Ive encountered which expects a list of users.

this workaround in using the user attribute name as a string instead of its given name is non ideal because now changing that value is separate from changing it anywhere else a user is specified or named

name = "${user.home}/${path}";
value = file user.name;
})
cfg.enableForUsers.users));
homeDirectory = (path: forEachUser {
inherit path;
file = (username: {
"d" = {
user = username;
group = "users";
mode = "0755";
};
});
});
in
{
# We need to create each of the folders before the next file otherwise parents get owned by root
"80-vscode-server-enable-for-users-create-config-folder" = homeDirectory ".config";
"81-vscode-server-enable-for-users-create-systemd-folder" = homeDirectory ".config/systemd";
"82-vscode-server-enable-for-users-create-systemd-user-folder" = homeDirectory ".config/systemd/user";
"83-vscode-server-enable-for-users-enable-auto-fix-vscode-server-service" = forEachUser {
path = ".config/systemd/user/auto-fix-vscode-server.service";
file = (username: {
"L+" = {
user = username;
group = "users";
# This path is made available by `services.vscode-server.enable = true;`
argument = "/run/current-system/etc/systemd/user/auto-fix-vscode-server.service";
};
});
};
};
})
]
)
37 changes: 23 additions & 14 deletions modules/vscode-server/home.nix
Original file line number Diff line number Diff line change
@@ -1,17 +1,26 @@
import ./module.nix ({
name,
description,
serviceConfig,
}: {
systemd.user.services.${name} = {
Unit = {
Description = description;
};
import ./module.nix (
{ name
, description
, serviceConfig
, cfg
, ...
}:
{
systemd.user.services.${name} = {
Unit = {
Description = description;
};

Service = serviceConfig;
Service = serviceConfig;

Install = {
WantedBy = [ "default.target" ];
Install = {
WantedBy = [ "default.target" ];
};
};
};
})

assertions = [{
assertion = !cfg.enableForUsers.enable;
message = "enableForUsers.enable=true doesn't make sense when using nixos-vscode-server as a home-manager module";
}];
}
)
36 changes: 33 additions & 3 deletions modules/vscode-server/module.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ moduleConfig: {
}: {
options.services.vscode-server = let
inherit (lib) mkEnableOption mkOption;
inherit (lib.types) lines listOf nullOr package str;
inherit (lib.types) lines listOf nullOr package str bool passwdEntry;
in {
enable = mkEnableOption "VS Code Server";
enable = mkEnableOption "VS Code Server autofix";

enableFHS = mkEnableOption "a FHS compatible environment";

Expand Down Expand Up @@ -49,14 +49,43 @@ moduleConfig: {
This can be used as a hook for custom further patching.
'';
};

enableForUsers = {
enable = mkOption {
type = bool;
default = false;
example = true;
description = ''
Ten0 marked this conversation as resolved.
Show resolved Hide resolved
Whether to enable the VS Code Server auto-fix service for each user.

This only makes sense if auto-fix-vscode-server is installed as a NixOS module.

This automatically sets up the service's symlinks for systemd in each users' home directory.

By default this will set it up for all regular users, but that list can be overridden by setting `users`.
'';
};

users = mkOption {
type = listOf (passwdEntry str);
default = builtins.attrNames (lib.attrsets.filterAttrs (username: userOptions: userOptions.isNormalUser) config.users.users);
defaultText = "builtins.attrNames (lib.filterAttrs (_: userOptions: userOptions.isNormalUser) config.users.users)";
example = [ "alice" "bob" ];
description = ''
List of users to enable the VS Code Server auto-fix service for.

By default this will fallback to the list of "normal" users.
'';
};
};
};

config = let
inherit (lib) mkDefault mkIf mkMerge;
cfg = config.services.vscode-server;
auto-fix-vscode-server =
pkgs.callPackage ../../pkgs/auto-fix-vscode-server.nix
(removeAttrs cfg [ "enable" ]);
(removeAttrs cfg [ "enable" "enableForUsers" ]);
in
mkIf cfg.enable (mkMerge [
{
Expand All @@ -74,6 +103,7 @@ moduleConfig: {
RestartSec = 0;
ExecStart = "${auto-fix-vscode-server}/bin/auto-fix-vscode-server";
};
inherit config cfg lib;
})
]);
}