@@ -937,34 +937,28 @@ fn do_user_switch(parent_exe: String) -> anyhow::Result<()> {
937937 Ok ( ( ) )
938938}
939939
940+ fn usage ( argv0 : & str ) -> ! {
941+ eprintln ! (
942+ r#"Usage: {} [switch|boot|test|dry-activate]
943+ switch: make the configuration the boot default and activate now
944+ boot: make the configuration the boot default
945+ test: activate the configuration, but don't make it the boot default
946+ dry-activate: show what would be done if this configuration were activated
947+ "# ,
948+ argv0
949+ ) ;
950+ std:: process:: exit ( 1 ) ;
951+ }
952+
940953/// Performs switch-to-configuration functionality for the entire system
941- fn do_system_switch ( ) -> anyhow:: Result < ( ) > {
954+ fn do_system_switch ( action : Action ) -> anyhow:: Result < ( ) > {
942955 let out = PathBuf :: from ( required_env ( "OUT" ) ?) ;
943956 let toplevel = PathBuf :: from ( required_env ( "TOPLEVEL" ) ?) ;
944957 let distro_id = required_env ( "DISTRO_ID" ) ?;
945958 let install_bootloader = required_env ( "INSTALL_BOOTLOADER" ) ?;
946959 let locale_archive = required_env ( "LOCALE_ARCHIVE" ) ?;
947960 let new_systemd = PathBuf :: from ( required_env ( "SYSTEMD" ) ?) ;
948961
949- let mut args = std:: env:: args ( ) ;
950- let argv0 = args. next ( ) . ok_or ( anyhow ! ( "no argv[0]" ) ) ?;
951-
952- let Some ( Ok ( action) ) = args. next ( ) . map ( |a| Action :: from_str ( & a) ) else {
953- eprintln ! (
954- r#"Usage: {} [switch|boot|test|dry-activate]
955- switch: make the configuration the boot default and activate now
956- boot: make the configuration the boot default
957- test: activate the configuration, but don't make it the boot default
958- dry-activate: show what would be done if this configuration were activated
959- "# ,
960- argv0
961- . split( std:: path:: MAIN_SEPARATOR_STR )
962- . last( )
963- . unwrap_or( "switch-to-configuration" )
964- ) ;
965- std:: process:: exit ( 1 ) ;
966- } ;
967-
968962 let action = ACTION . get_or_init ( || action) ;
969963
970964 // The action that is to be performed (like switch, boot, test, dry-activate) Also exposed via
@@ -1923,13 +1917,26 @@ won't take effect until you reboot the system.
19231917}
19241918
19251919fn main ( ) -> anyhow:: Result < ( ) > {
1926- match (
1927- unsafe { nix:: libc:: geteuid ( ) } ,
1928- std:: env:: var ( "__NIXOS_SWITCH_TO_CONFIGURATION_PARENT_EXE" ) . ok ( ) ,
1929- ) {
1930- ( 0 , None ) => do_system_switch ( ) ,
1931- ( 1 ..=u32:: MAX , None ) => bail ! ( "This program does not support being ran outside of the switch-to-configuration environment" ) ,
1932- ( _, Some ( parent_exe) ) => do_user_switch ( parent_exe) ,
1920+ match std:: env:: var ( "__NIXOS_SWITCH_TO_CONFIGURATION_PARENT_EXE" ) . ok ( ) {
1921+ Some ( parent_exe) => do_user_switch ( parent_exe) ,
1922+ None => {
1923+ let mut args = std:: env:: args ( ) ;
1924+ let argv0 = args. next ( ) . ok_or ( anyhow ! ( "no argv[0]" ) ) ?;
1925+ let argv0 = argv0
1926+ . split ( std:: path:: MAIN_SEPARATOR_STR )
1927+ . last ( )
1928+ . unwrap_or ( "switch-to-configuration" ) ;
1929+
1930+ let Some ( Ok ( action) ) = args. next ( ) . map ( |a| Action :: from_str ( & a) ) else {
1931+ usage ( & argv0) ;
1932+ } ;
1933+
1934+ if unsafe { nix:: libc:: geteuid ( ) } == 0 {
1935+ do_system_switch ( action)
1936+ } else {
1937+ bail ! ( "{} must be run as the root user" , argv0) ;
1938+ }
1939+ }
19331940 }
19341941}
19351942
0 commit comments