Skip to content

Commit

Permalink
[FC-32491] get rid of userenv
Browse files Browse the repository at this point in the history
The old userenv module relied on nix envs, which are prone to breakage
on system upgrades. the new approach will function the same, but
eliminate the possibility of breakage while also being more discoverable
in the `/etc/local/nixos` directory
  • Loading branch information
PhilTaken committed Oct 23, 2023
1 parent 7c7d8d1 commit 226b04f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 35 deletions.
25 changes: 12 additions & 13 deletions src/batou_ext/nix.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,37 +102,36 @@ class UserEnv(batou.component.Component):
"yarn",
"zip",
],
channel="<nixos-channel-base-url-to-be-put-in>/nixexprs.tar.xz"
shellInit="# additional shell init")
A list of available channels can be found at e.g.
https://nixos.org/channels/
"""

namevar = "profile_name"
channel = batou.component.Attribute(str)
shellInit = ""
packages = ()
let_extra = ""
channel = None

def configure(self):
self.checksum = hashlib.sha256()
if self.channel:
batou.output.step(
"WARNING",
"using `channel` with UserEnv is deprecated, please remove it from your deployment code.",
yellow=True,
)

template = pkg_resources.resource_string(
__name__, "resources/userenv.nix"
).decode("UTF-8")
self.profile = self.expand(template).encode("UTF-8")
self.checksum.update(self.profile)
self.nix_env_name = self.expand(
"{{component.profile_name}}-1.{{component.checksum.hexdigest()}}"
)

self += batou.lib.file.File(
self.profile_name + ".nix", content=self.profile
)
self += Package(self.nix_env_name, file=self._.path)
self.user_profile_path = os.path.expanduser(
"~/.nix-profile/etc/profile.d/{}.sh".format(self.profile_name)
f"/etc/local/nixos/profile-{self.profile_name}.nix",
content=self.profile,
)
self.user_bin_path = os.path.expanduser("~/.nix-profile/bin")
self.user_profile_path = "/etc/profile"


class Rebuild(batou.component.Component):
Expand Down
31 changes: 9 additions & 22 deletions src/batou_ext/resources/userenv.nix
Original file line number Diff line number Diff line change
@@ -1,27 +1,14 @@
let
# see https://nixos.org/channels/
nixpkgs = fetchTarball {{component.channel}};

in
{ pkgs ? import nixpkgs { } }:

with pkgs;
let
{ pkgs, ... }: let
{{component.let_extra}}

shellInit = writeText "shellInit"''
in {
users.users."{{component.environment.service_user}}".packages = with pkgs; [
# {%- for name in component.packages %}
{{name}}
# {%- endfor %}
];

environment.shellInit = ''
{{component.shellInit.replace('\n', '\n ').strip()}}
'';

{{component.let_extra}}

in buildEnv {
name = "{{ '{{' }}component.nix_env_name{{ '}}' }}";
paths = [
(runCommand "profile" { } "install -D ${shellInit} $out/etc/profile.d/{{component.profile_name}}.sh")
{%- for name in component.packages %}
{{name}}
{%- endfor %}
];
extraOutputsToInstall = [ "bin" "dev" "lib" "man" "out" ];
}

0 comments on commit 226b04f

Please sign in to comment.