`nix-channel` fails after trying to set permissions to a path

error: could not set permissions on '/nix/var/nix/profiles/per-user' to 755: Operation not permitted

I’ve seen this error on other posts when searching on the internet for a solution, but my situation is different.

This error gets thrown while the command gets ran inside an activation script through su. Here’s my activation script:

system.activationScripts.setup.text =
''
# Check if it's the first time the script ran
if [ -e /etc/nixos/.setup-done ]
then exit
else
  # Variables
  ghlink="https://github.com/Andy3153"
  git="${pkgs.git}/bin/git"
  su="${pkgs.su}/bin/su"
  nix_channel="${pkgs.nix}/bin/nix-channel"
  nix_shell="${pkgs.nix}/bin/nix-shell"

  # Create folder structure
  mkdir -p /home/andy3153/src
  cd /home/andy3153/src

  mkdir -p hyprland/hyprland-rice
  mkdir -p nixos/nixos-rice
  mkdir -p nvim/andy3153-init.lua
  mkdir -p sh/andy3153-zshrc

  # Clone Git repos
  $git clone $ghlink/hyprland-rice hyprland/hyprland-rice
  $git clone $ghlink/nixos-rice nixos/nixos-rice
  $git clone $ghlink/andy3153-init.lua nvim/andy3153-init.lua
  $git clone $ghlink/andy3153-zshrc sh/andy3153-zshrc

  # Link NixOS configs in their place
  rm -r /etc/nixos
  ln -s /home/andy3153/src/nixos/nixos-rice/etc/nixos /etc/

  # Link home-manager configs in their place
  rm -r /home/andy3153/.config/home-manager
  ln -s /home/andy3153/src/nixos/nixos-rice/home/andy3153/.config/home-manager/ ~andy3153/.config/

  # Install Home Manager for andy3153
  $su andy3153 --shell ${pkgs.runtimeShell} --command "\
    $nix_channel --add https://github.com/nix-community/home-manager/archive/release-23.05.tar.gz home-manager && \
    $nix_channel --update && \
    $nix_shell \<home-manager\> -A install \
  "

  # Make sure andy3153 owns his files
  chown -R andy3153:andy3153 /home/andy3153

  # Ensure it's the last time the script runs
  touch /etc/nixos/.setup-done
fi

See the entire configuration.nix file here.

I’m not completely sure what to do. I tried making the script do a chmod before the nix-channel command tries to and it did not work, and I also tried the most random thing I could’ve thought about:

export PATH=${pkgs.coreutils}/bin:$PATH

because I thought there might be a possibility that nix-channel could not find the chmod program in $PATH, but it still gives the same error.

How do I fix this?