How to use home.activation to run arbitrary commands?

home.activation seems to be a way to run arbitrary shell commands. In my case (for test purposes, just to get this working), I want to create an empty directory in my home directory. According to the documentation, I think this should do the trick:

  home-manager.users.media = { pkgs, ... }: {
    <SNIP>
    home.activation = {
      test = lib.hm.dag.entryAfter ["writeBoundary"] ''
        mkdir testdir
      '';
    };

However, I get this error:

<SNIP>
       error: attribute 'hm' missing

       at /etc/nixos/media.nix:55:14:

           54|     home.activation = {
           55|       test = lib.hm.dag.entryAfter ["writeBoundary"] ''
             |              ^
           56|         mkdir testdir
       Did you mean one of id or or?

I did some searching and found this reddit post that indicates I should add home-manager before lib:

  home-manager.users.media = { pkgs, ... }: {
    <SNIP>
    home.activation = {
      test = home-manager.lib.hm.dag.entryAfter ["writeBoundary"] ''
        mkdir testdir
      '';
    };

However:

<SNIP>
       error: undefined variable 'home-manager'

       at /etc/nixos/media.nix:55:14:

           54|     home.activation = {
           55|       test = home-manager.lib.hm.dag.entryAfter ["writeBoundary"] ''
             |              ^
           56|         mkdir testdir

What am I missing or doing wrong?

Hard to say without full config, but most likely you just need to add lib to module args:

{ pkgs, lib, ... }:

So HM uses it’s extended lib inside.

Unless you don’t have home-manager module at all, then you should add it.
But if everything else related to HM works it’s unlikely.

1 Like

Ah ha! That was exactly the problem. I had lib defined at the top of my nix file where all my user settings are defined:

{ config, lib, pkgs, modulesPath, ... }:

but, the home-manager invocation as shown above was missing it:

home-manager.users.media = { pkgs, ... }: {

Simply adding lib after pkgs resolved it. Thanks, @ghpzin!

1 Like