Skip to content

Commit a04361c

Browse files
committed
Add option to enable the service for all users
1 parent fc900c1 commit a04361c

File tree

4 files changed

+139
-27
lines changed

4 files changed

+139
-27
lines changed

README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@ you'll have to manually enable the service for each user (see below).
4242

4343
#### Enable the service
4444

45+
##### Automatically for all users
46+
47+
Instead of just
48+
```nix
49+
{ services.vscode-server.enable = true; }
50+
```
51+
52+
use:
53+
```nix
54+
{
55+
services.vscode-server = {
56+
enable = true;
57+
enableForAllUsers = true;
58+
};
59+
}
60+
```
61+
62+
This will use `tmpfiles` to setup the permanent symlink described below for each regular user.
63+
64+
##### Manually for each user
65+
4566
And then enable them for the relevant users:
4667

4768
```bash
@@ -79,6 +100,8 @@ ln -sfT /run/current-system/etc/systemd/user/auto-fix-vscode-server.service ~/.c
79100

80101
### Home Manager
81102

103+
#### Install as a tarball
104+
82105
Put this code into your [home-manager](https://github.com/nix-community/home-manager) configuration i.e. in `~/.config/nixpkgs/home.nix`:
83106

84107
```nix
@@ -91,6 +114,25 @@ Put this code into your [home-manager](https://github.com/nix-community/home-man
91114
}
92115
```
93116

117+
#### Install as a flake
118+
119+
```nix
120+
{
121+
inputs.vscode-server.url = "github:nix-community/nixos-vscode-server";
122+
123+
outputs = { self, vscode-server, home-manager }: {
124+
homeConfigurations.yourhostname = home-manager.lib.homeManagerConfiguration {
125+
modules = [
126+
vscode-server.homeModules.default
127+
({ config, pkgs, ... }: {
128+
services.vscode-server.enable = true;
129+
})
130+
];
131+
};
132+
};
133+
}
134+
```
135+
94136
## Usage
95137

96138
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.

modules/vscode-server/default.nix

Lines changed: 57 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,57 @@
1-
import ./module.nix ({
2-
name,
3-
description,
4-
serviceConfig,
5-
}: {
6-
systemd.user.services.${name} = {
7-
inherit description serviceConfig;
8-
wantedBy = [ "default.target" ];
9-
};
10-
})
1+
import ./module.nix (
2+
{ name
3+
, description
4+
, serviceConfig
5+
, lib
6+
, config
7+
, cfg
8+
}: lib.mkMerge [
9+
{
10+
systemd.user.services.${name} = {
11+
inherit description serviceConfig;
12+
wantedBy = [ "default.target" ];
13+
};
14+
}
15+
(lib.mkIf cfg.enableForAllUsers {
16+
systemd.tmpfiles.settings =
17+
let
18+
forEachUser = ({ path, file }: lib.attrsets.mapAttrs'
19+
(username: userOptions:
20+
{
21+
# Create the directory so that it has the appropriate permissions if it doesn't already exist
22+
# Otherwise the directive below creating the symlink would have that owned by root
23+
name = "${userOptions.home}/${path}";
24+
value = file username;
25+
})
26+
(lib.attrsets.filterAttrs (username: userOptions: userOptions.isNormalUser) config.users.users));
27+
homeDirectory = (path: forEachUser {
28+
inherit path;
29+
file = (username: {
30+
"d" = {
31+
user = username;
32+
group = "users";
33+
mode = "0755";
34+
};
35+
});
36+
});
37+
in
38+
{
39+
# We need to create each of the folders before the next file otherwise parents get owned by root
40+
"80-setup-config-folder-for-all-users" = homeDirectory ".config";
41+
"81-setup-systemd-folder-for-all-users" = homeDirectory ".config/systemd";
42+
"82-setup-systemd-user-folder-for-all-users" = homeDirectory ".config/systemd/user";
43+
"83-enable-auto-fix-vscode-server-service-for-all-users" = forEachUser {
44+
path = ".config/systemd/user/auto-fix-vscode-server.service";
45+
file = (username: {
46+
"L+" = {
47+
user = username;
48+
group = "users";
49+
# This path is made available by `services.vscode-server.enable = true;`
50+
argument = "/run/current-system/etc/systemd/user/auto-fix-vscode-server.service";
51+
};
52+
});
53+
};
54+
};
55+
})
56+
]
57+
)

modules/vscode-server/home.nix

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
1-
import ./module.nix ({
2-
name,
3-
description,
4-
serviceConfig,
5-
}: {
6-
systemd.user.services.${name} = {
7-
Unit = {
8-
Description = description;
9-
};
1+
import ./module.nix (
2+
{ name
3+
, description
4+
, serviceConfig
5+
, cfg
6+
, ...
7+
}:
8+
{
9+
systemd.user.services.${name} = {
10+
Unit = {
11+
Description = description;
12+
};
1013

11-
Service = serviceConfig;
14+
Service = serviceConfig;
1215

13-
Install = {
14-
WantedBy = [ "default.target" ];
16+
Install = {
17+
WantedBy = [ "default.target" ];
18+
};
1519
};
16-
};
17-
})
20+
21+
assertions = [{
22+
assertion = !cfg.enableForAllUsers;
23+
message = "enableForAllUsers=true doesn't make sense when using nixos-vscode-server as a home-manager module";
24+
}];
25+
}
26+
)

modules/vscode-server/module.nix

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@ moduleConfig: {
66
}: {
77
options.services.vscode-server = let
88
inherit (lib) mkEnableOption mkOption;
9-
inherit (lib.types) lines listOf nullOr package str;
9+
inherit (lib.types) lines listOf nullOr package str bool;
1010
in {
11-
enable = mkEnableOption "VS Code Server";
11+
enable = mkEnableOption "VS Code Server autofix";
1212

1313
enableFHS = mkEnableOption "a FHS compatible environment";
1414

@@ -49,14 +49,27 @@ moduleConfig: {
4949
This can be used as a hook for custom further patching.
5050
'';
5151
};
52+
53+
enableForAllUsers = mkOption {
54+
type = bool;
55+
default = false;
56+
example = true;
57+
description = ''
58+
Whether to enable the VS Code Server auto-fix service for all users.
59+
60+
This only makes sense if auto-fix-vscode-server is installed as a NixOS module.
61+
62+
This automatically sets up the service's symlinks for systemd in each users' home directory.
63+
'';
64+
};
5265
};
5366

5467
config = let
5568
inherit (lib) mkDefault mkIf mkMerge;
5669
cfg = config.services.vscode-server;
5770
auto-fix-vscode-server =
5871
pkgs.callPackage ../../pkgs/auto-fix-vscode-server.nix
59-
(removeAttrs cfg [ "enable" ]);
72+
(removeAttrs cfg [ "enable" "enableForAllUsers" ]);
6073
in
6174
mkIf cfg.enable (mkMerge [
6275
{
@@ -74,6 +87,7 @@ moduleConfig: {
7487
RestartSec = 0;
7588
ExecStart = "${auto-fix-vscode-server}/bin/auto-fix-vscode-server";
7689
};
90+
inherit config cfg lib;
7791
})
7892
]);
7993
}

0 commit comments

Comments
 (0)