Skip to content

Installation for nixos based raspberry pi #4

Description

@joehealy

Extracted the polling version in a form suitable for nixos (could use the recommended version once RPi.GPIO has support for aarch64):

Save the following as /etc/nixos/shutdown-button.nix, then add it as an import into /etc/nixos/configuration.nix (ie add ./shutdown-button.nix as a line in the imports list) and run nixos-rebuild switch.

{pkgs, config, ... }:
  
let 

  shutdownCheck = pkgs.writeShellScriptBin "shutdownCheck" ''

# ATXRaspi/MightyHat interrupt based shutdown/reboot script
# Script by Felix Rusu
#This is GPIO 7 (pin 26 on the pinout diagram).
#This is an input from ATXRaspi to the Pi.
#When button is held for ~3 seconds, this pin will become HIGH signalling to this script to poweroff the Pi.
SHUTDOWN=465
REBOOTPULS {pkgs, config, ... }:
  
let 

  shutdownCheck = pkgs.writeShellScriptBin "shutdownCheck" ''

# ATXRaspi/MightyHat interrupt based shutdown/reboot script
# Script by Felix Rusu
#This is GPIO 7 (pin 26 on the pinout diagram).
#This is an input from ATXRaspi to the Pi.
#When button is held for ~3 seconds, this pin will become HIGH signalling to this script to poweroff the Pi.
SHUTDOWN=465
REBOOTPULSEMINIMUM=200      #reboot pulse signal should be at least this long
REBOOTPULSEMAXIMUM=600      #reboot pulse signal should be at most this long
echo "$SHUTDOWN" > /sys/class/gpio/export
echo "in" > /sys/class/gpio/gpio$SHUTDOWN/direction
#Added reboot feature (with ATXRaspi R2.6 (or ATXRaspi 2.5 with blue dot on chip)
#Hold ATXRaspi button for at least 500ms but no more than 2000ms and a reboot HIGH pulse of 500ms length will be issued
#This is GPIO 8 (pin 24 on the pinout diagram).
#This is an output from Pi to ATXRaspi and signals that the Pi has booted.
#This pin is asserted HIGH as soon as this script runs (by writing "1" to /sys/class/gpio/gpio8/value)
BOOT=466
echo "$BOOT" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio$BOOT/direction
echo "1" > /sys/class/gpio/gpio$BOOT/value
echo -e "\n=========================================================================================="
echo "   ATXRaspi shutdown POLLING script started: asserted pins ($SHUTDOWN=input,LOW; $BOOT=output,HIGH)"
echo "   Waiting for GPIO$SHUTDOWN to become HIGH (short HIGH pulse=REBOOT, long HIGH=SHUTDOWN)..."
echo "=========================================================================================="
#This loop continuously checks if the shutdown button was pressed on ATXRaspi (GPIO7 to become HIGH), and issues a shutdown when that happens.
#It sleeps as long as that has not happened.
while [ 1 ]; do
  shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
  if [ $shutdownSignal = 0 ]; then
    ${pkgs.coreutils}/bin/sleep 0.2
  else  
    pulseStart=$(date +%s%N | cut -b1-13) # mark the time when Shutoff signal went HIGH (milliseconds since epoch)
    while [ $shutdownSignal = 1 ]; do
      ${pkgs.coreutils}/bin/sleep 0.02
      if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMAXIMUM ]; then
        echo -e "\n====================================================================================="
        echo "            SHUTDOWN request from GPIO", SHUTDOWN, ", halting Rpi ..."
        echo "====================================================================================="
        poweroff
        exit
      fi
      shutdownSignal=$(cat /sys/class/gpio/gpio$SHUTDOWN/value)
    done
    #pulse went LOW, check if it was long enough, and trigger reboot
    if [ $(($(date +%s%N | cut -b1-13)-$pulseStart)) -gt $REBOOTPULSEMINIMUM ]; then 
      echo -e "\n====================================================================================="
      echo "            REBOOT request from GPIO", SHUTDOWN, ", recycling Rpi ..."
      echo "====================================================================================="
      reboot
      exit
    fi
  fi
done

'';



in {


  systemd.services.shutdown-button = {
    description = "Shutdown Button";

    after = [ "multi-user.target" ];
    wantedBy = [ "multi-user.target" ];

    serviceConfig = {
      User = "root";
      ExecStart = "${shutdownCheck}/bin/shutdownCheck";
      Restart = "always";
    };
  };


}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions