diff --git a/README.md b/README.md index 46f2b66..1099eab 100644 --- a/README.md +++ b/README.md @@ -42,6 +42,27 @@ 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; + enableForAllUsers = true; + }; +} +``` + +This will use `tmpfiles` to setup the permanent symlink described below for each regular user. + +##### Manually for each user + And then enable them for the relevant users: ```bash @@ -79,6 +100,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 @@ -91,6 +114,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. diff --git a/modules/vscode-server/default.nix b/modules/vscode-server/default.nix index 6e9b419..4773a4b 100644 --- a/modules/vscode-server/default.nix +++ b/modules/vscode-server/default.nix @@ -1,10 +1,57 @@ -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.enableForAllUsers { + systemd.tmpfiles.settings = + let + forEachUser = ({ path, file }: lib.attrsets.mapAttrs' + (username: userOptions: + { + # Create the directory so that it has the appropriate permissions if it doesn't already exist + # Otherwise the directive below creating the symlink would have that owned by root + name = "${userOptions.home}/${path}"; + value = file username; + }) + (lib.attrsets.filterAttrs (username: userOptions: userOptions.isNormalUser) config.users.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-setup-config-folder-for-all-users" = homeDirectory ".config"; + "81-setup-systemd-folder-for-all-users" = homeDirectory ".config/systemd"; + "82-setup-systemd-user-folder-for-all-users" = homeDirectory ".config/systemd/user"; + "83-enable-auto-fix-vscode-server-service-for-all-users" = 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"; + }; + }); + }; + }; + }) + ] +) diff --git a/modules/vscode-server/home.nix b/modules/vscode-server/home.nix index 616145b..4dd2665 100644 --- a/modules/vscode-server/home.nix +++ b/modules/vscode-server/home.nix @@ -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.enableForAllUsers; + message = "enableForAllUsers=true doesn't make sense when using nixos-vscode-server as a home-manager module"; + }]; + } +) diff --git a/modules/vscode-server/module.nix b/modules/vscode-server/module.nix index 4bf714b..ec9af77 100644 --- a/modules/vscode-server/module.nix +++ b/modules/vscode-server/module.nix @@ -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; in { - enable = mkEnableOption "VS Code Server"; + enable = mkEnableOption "VS Code Server autofix"; enableFHS = mkEnableOption "a FHS compatible environment"; @@ -49,6 +49,19 @@ moduleConfig: { This can be used as a hook for custom further patching. ''; }; + + enableForAllUsers = mkOption { + type = bool; + default = false; + example = true; + description = '' + Whether to enable the VS Code Server auto-fix service for all users. + + 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. + ''; + }; }; config = let @@ -56,7 +69,7 @@ moduleConfig: { cfg = config.services.vscode-server; auto-fix-vscode-server = pkgs.callPackage ../../pkgs/auto-fix-vscode-server.nix - (removeAttrs cfg [ "enable" ]); + (removeAttrs cfg [ "enable" "enableForAllUsers" ]); in mkIf cfg.enable (mkMerge [ { @@ -74,6 +87,7 @@ moduleConfig: { RestartSec = 0; ExecStart = "${auto-fix-vscode-server}/bin/auto-fix-vscode-server"; }; + inherit config cfg lib; }) ]); }