Create /usr/lib/systemd/system/mac-keyboard.service with the following contents:
[Unit]
Description=mac-keyboard
[Service]
Type=oneshot
ExecStart=/bin/sh -c "echo 2 > /sys/module/hid_apple/parameters/fnmode"
ExecStop=/bin/sh -c "echo 1 > /sys/module/hid_apple/parameters/fnmode"
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
Run the following commands to enable and start the service:
sudo systemctl daemon-reload && sudo systemctl enable --now mac-keyboard.service